6

I have a firebase cloud function which will be invoked on HTTP request which is working fine.

Now, I want to read data from a JSON file for some business logic. Below are the 2 ways I was trying to read the JSON file:

Option #1) Saved the JSON file inside 'public' directory in my nodejs project and deployed. Got a Hosting URL which I am using like below. But its throwing an error saying 'Error: getaddrinfo ENOTFOUND...'

Option #2) Uploaded the JSON file to firebase cloud storage. Didnt find any example to try this out. Ended up with the below code:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Firestore = require('@google-cloud/firestore');
const firestore = new Firestore();
const http = require('http');
const url = require('url');

// Option #2 required variables
var storage = require('@google-cloud/storage');
const gcs = storage({projectId: "<Project ID>"});
const bucket = gcs.bucket("<bucket-name>");
const file = bucket.file("<filename.json>")

// HTTP Trigger
exports.functionName = functions.https.onRequest((req, res) => {
 var request = require('request'); 
 var paramValue = req.body.queryParam;
 console.log(paramValue);

// Option #1 - Using hosted URL
var hostingURL = "https://xxxxxxxx.firebaseapp.com/filename.json";
 console.log(hostingURL);
 request({
        url:hostingURL,
        method: 'POST', 
        json:{ key: 'value' } },function(error, response, data) {
 });
  
// Option #2 - Ended up here. Want to read from cloud storage bucket.
console.log(file);

});

Can some one help me?

grrigore
  • 1,080
  • 1
  • 17
  • 29
Naveen
  • 414
  • 1
  • 4
  • 15
  • Please edit your question to say more about the how your function is supposed to work. All you're really saying is that you tried some stuff, and that stuff didn't work the way you expect. What exactly do you need to accomplish other than just "reading a JSON file", which is kind of vague. – Doug Stevenson Jun 29 '18 at 15:59
  • My cloud function is a HTTP trigger which can be invoked as "https://us-central1-xxxxx.cloudfunctions.net/?queryParam= ". I am getting the 'paramValue' values as 'request.query.queryParam. Now, I need to return success if that 'paramValue' exists in the JSON file. In order to compare the 'paramValue' with data in JSON file, I need to read the JSON file inside my cloud function. Please let me know if you need any further details. – Naveen Jun 29 '18 at 16:07
  • https://stackoverflow.com/questions/42774807/cloud-functions-for-firebase-getaddrinfo-enotfound – Doug Stevenson Jun 29 '18 at 16:17
  • Thanks @DougStevenson. Since I want to use free plan and demo it to business, Is there a way to implement the option #2? – Naveen Jun 29 '18 at 16:22
  • Yes, it's possible. – Doug Stevenson Jun 29 '18 at 16:22
  • Can you please refer to an example (or) share the code snippet? – Naveen Jun 29 '18 at 16:23
  • Google search should yield documentation and samples. – Doug Stevenson Jun 29 '18 at 16:24
  • I am doing google search from past 24hrs for the 2nd option and I didnt find any code sample (or) documentation, Can you please share any link – Naveen Jun 29 '18 at 16:25

2 Answers2

12

You can place the .json file in the same folder, where your index.js is. Then you can do the following:

const config = require('./config.json');
console.log(config.foo);

Given following config.json file:

{
    "foo" : "bar"
}
Thomas
  • 1,716
  • 2
  • 13
  • 28
  • Thanks @Thomas, your solution works fine. Do you also know how to read it from firebase cloud storage bucket (as I mentioned in option #2) ? If so, can you please share the code snippet. – Naveen Jun 29 '18 at 17:24
  • Do you know if this is secure? I'm thinking about storing a client secret string in a JSON file like this for my Firebase function to use. If someone wanted to view that file, they'd have to break into Google's servers, right? – Xavier L. Sep 05 '18 at 15:19
  • Actually I found the solution here: https://firebase.google.com/docs/functions/config-env. Google's documentation even says it's for API keys and stuff in basically the first paragraph. – Xavier L. Sep 06 '18 at 00:37
  • 1
    Error: Cannot find module './database.json' – pete Aug 01 '20 at 11:15
  • In my case "Cannot find module" error was due to the use of typescript instead of javascript. I solved this by moving the json file from the src directory to the lib one, where js files reside. – marcolav Apr 27 '21 at 10:24
5

If your file is in Firebase Could Storage you can use this approach:

const admin = require('firebase-admin');
admin.storage().bucket().file("yourDirForFile/yourFile.json")
    .download(function (err, contents) {
        if (!err) {
            var jsObject = JSON.parse(contents.toString('utf8'))
        }
});

variable jsObject you can use as you wish. It is in memory for now.

gabhor
  • 541
  • 8
  • 21
AlexM
  • 101
  • 1
  • 5