Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a JSON file data and use it in firebase cloud function

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?

like image 845
Naveen Avatar asked Jun 29 '18 14:06

Naveen


People also ask

Can I use EJS with Firebase?

There is no reason to Firebase not work with EJS or even DigitalOcean. If you are integrating Firebase in front-end your problem is not back-end related.

Can I host dynamic website on Firebase?

Firebase Hosting provides fast and secure hosting for your web app, static and dynamic content, and microservices.

Is it possible to read a JSON file from a firebase?

Yes, it's possible. 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); 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) ?

How to read JSON data from a URL in JavaScript?

The JSON files can also be read from URLs. However, several methods are available to read JSON data from a URL in Javascript like jquery, loadJSON, etc. But we will use the loadJSON () function to read a JSON file from the URL.

How to read a JSON file from an external server?

The fetch API is the preferable method to use when we want to read a JSON file either from an external server or local file into our JavaScript file. Another method we can use aside from making an HTTP request is the import statement. This method has a few complications, but we will address them all.

How do I read a JSON file in Python?

One standard method we can use to read a JSON file (either a local file or one uploaded to a server) is with the Fetch API. It uses the same syntax for both. The only difference would be the URL. For example, suppose we have a local file within our project's folder named data.json that contains the following JSON data:


2 Answers

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"
}
like image 167
Thomas Avatar answered Oct 18 '22 10:10

Thomas


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.

like image 39
AlexM Avatar answered Oct 18 '22 09:10

AlexM