Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase function to return Json

It is a simple requirement - how do I return the entire json from a firebase database.

My function is as such [index.js]

const functions = require('firebase-functions');

const admin = require('firebase-admin');
var serviceAccount = require('./xxMyKeyxx.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: 'https://xxmyProjectxx.firebaseio.com'
});


exports.myQuery = functions.https.onRequest((req, res) => {
  // Grab the text parameter.
  const original = req.query.text;


  return admin.database().ref('/myFbDatabase').once.xxx
  I lost it from here. 

what is the syntax to return the Json that is sitting in my FB database /myFbDatabase ? });

like image 222
ChzDz Avatar asked Feb 24 '18 18:02

ChzDz


Video Answer


1 Answers

Subscribing to the 'value' event will give you the entire database

exports.myQuery = functions.https.onRequest((req, res) => {
   // Grab the text parameter.
   const original = req.query.text;

   admin
     .database()
     .ref('/myFbDatabase')
     .once('value', 
       snap =>  res.json(snap.val()),
       err => res.json(err)
     )
})
like image 107
LazyElephant Avatar answered Oct 14 '22 02:10

LazyElephant