I
I
Ivan Pavlov2017-07-16 22:06:06
Node.js
Ivan Pavlov, 2017-07-16 22:06:06

How to get data from Firebase Database through Firebase Admin in Firebase Functions?

Task:
Using the Firebase Functions http trigger, get all records in the Firebase Database once, that is, through the once method.
The code:

const functions = require('firebase-functions');
const admin = require("firebase-admin");
const serviceAccount = require("path/to/serviceAccount.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://<database>.firebaseio.com/"
});

var db = admin.database();
var ref = db.ref("/data");

exports.resend = functions.https.onRequest((request, response) => {
  return ref.once("value").then((data) => {
    console.log('done');
    response.status(200).send("OK");
  }).catch((error) => {
    console.log('error', error);
    response.status(500).send("Error");
  });
});

The essence of the problem:
The error, in fact, is the following: Firebase Functions breaks the connection by timeout, I think that because of the inability to access the database, because none of the then, catch methods are entered and the data is not retrieved from the database. The instructions show code that doesn't contain return ref.., but that doesn't help either. Tested locally and on Google servers - the result is the same, timeout. I tried to issue manually write and read permissions in ServiceAccount - the same thing. In addition, I tried to initialize admin via functions.config().firebase, which also did not give any result.
Question:
Whether precisely business in the rights to reading? Because the data writing is successful, though it does not want to become a Promise, the result is the same as when reading - timeout. Is there something wrong in the code above? Maybe there is some kind of instruction specifically for this task? Or are there any restrictions on the organization of reading data from the database in Firebase Functions? How to organize reading from the database once by http trigger in Firebase Functions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Pavlov, 2017-07-20
@iPaf

As a result, I decided to create a base listener separately from the method that saves all the data to an array, and analyze the data in the array using an HTTP trigger. Thanks to all! Code below.

ref.on("value", function(snapshots) {
  data = [];
  
  snapshots.forEach(function(record) {
    var val = record.val();
    var value = val.value;
    
    data.push({ value: value });
  });
});

exports.resend = functions.https.onRequest((request, response) => {
  var query = request.query;
  var value = query.value;

  for (var i = 0; i < data.length; i++)
  {
    var item = data[i];
    
    if (item['value'] == value)
      response.status(200).send("OK");
  }
  
  response.status(500).send("Value not found");
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question