25

I have a Firebase HTTPs function. The function needs to read a value from a Firebase database based on the query parameter, and return a result based on this data.

The Firebase JS SDK says to do this using:

return firebase.database().ref('/users/' + userId).once('value').then(function(snapshot) {
  var username = snapshot.val().username;
  // ...
});

However, the Cloud functions examples have:

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

functions.database.ref('/');

But the DB reference doesn't have the method once, only onWrite (https://firebase.google.com/docs/reference/functions/functions.database.RefBuilder). This is obviously for DB write functions, rather than HTTP functions.

Is there a correct way to read from the database once in a HTTP function? Can I use the normal Firebase SDK, or is there a better way?

Thanks.

2 Answers 2

50

I found the solution in combining the answer here on how to get the parameter and an answer from Michael Blight to How to run query from inside of Cloud function?

The answer there also shows what is required to use firebase-admin.

The following works for me when calling my-project.firebaseapp.com/event/123/.

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

admin.initializeApp(functions.config().firebase);

exports.showEvent = functions.https.onRequest((req, res) => {
    const params = req.url.split("/");
    const eventId = params[2];
    return admin.database().ref('events/' + eventId).once('value', (snapshot) => {
        var event = snapshot.val();
        res.send(`
            <!doctype html>
            <html>
                <head>
                    <title>${event.name}</title>
                </head>
                <body>
                    <h1>Title ${event. name} in ${event.city}</h1>
                </body>
            </html>`
        );
     });
});
Sign up to request clarification or add additional context in comments.

1 Comment

How can i get the data that i return at the end? i cannot understand it
19

You're confusing two parts:

  • the firebase-functions module, which contains the logic to trigger based on database calls with functions.database.ref('/path').onWrite().
  • the firebase-admin module, which allows your function to call into the database.

Since you have a HTTP function, you should trigger as the documentation for HTTP functions shows:

exports.data = functions.https.onRequest((req, res) => {
  // ...
});

Then in your function, you access the database as the documentation for the Admin SDK shows:

return admin.database().ref('/users/' + userId).once('value').then(function(snapshot) {
  var username = snapshot.val().username;
  // ...
});

So in total:

exports.date = functions.https.onRequest((req, res) => {
  admin.database().ref('/users/' + userId).once('value').then(function(snapshot) {
    var username = snapshot.val().username;
    res.status(200).send(username);
  });
});

Note that this is a tricky pattern. The call to the database happens asynchronously and may take some time to complete. While waiting for that, the HTTP function may time out and be terminated by the Google Cloud Functions system. See this section of the documentation.

As a general rule I'd recommend using a Firebase Database SDK or its REST API to access the database and not rely on a HTTP function as middleware.

9 Comments

Does the firebase-admin automatically authenticate in a function? I need to get information from the database and render a HTML partial based on the data, so using the Firebase DB REST api isn't really an option here unfortunately.
I think you are missing some returns in your answer. Getting ESLint errors about the promise expecting a return or try/catch block.
Also it should be admin.database because in firebase functions you use const admin = require('firebase-admin');
@Badrush, you have no idea how many hours of work you saved me. Without admin.database().ref(.....) it kept giving errors
@user3833732 I was in the same spot as you. Spent two full days trying to figure out how to get firebase functions to get data from the db and then serve a file from firebase storage. Finally things started to make sense and I caught this error. Glad I could help out =)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.