Home > Net >  In Firebase realtime database, If i get link of database url at run time. Then how to fetch data fro
In Firebase realtime database, If i get link of database url at run time. Then how to fetch data fro

Time:07-15

I get the database at the runtime of the function. So how to fetch data from that data base or how to make ref of that database.Like in below code i use the database url which i have knowledge but if i get the url of database as a request. Then how i can use it.

var admin = require("firebase-admin");

// Initialize the app with a null auth variable, limiting the server's access
admin.initializeApp({
  // The database URL depends on the location of the database
  databaseURL: "https://flysample-75b81.firebaseio.com",
  databaseAuthVariableOverride: null
});

// The app only has access to public data as defined in the Security Rules
let db = admin.database();

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions

exports.getDataFromDatabase = functions.https.onRequest((request, response) => {
    let nodeRef = request.query.rootNode;
    let ref = db.ref(`${nodeRef}`);
    ref.once('value', (snapshot)=> {
        response.send(snapshot.val());
    })
});```

CodePudding user response:

If you get the URL from the request that triggers the Cloud Function, you can call initializeApp and create the database object inside that function:

exports.getDataFromDatabase = functions.https.onRequest((request, response) => {
  const url = ....; //            
  • Related