Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access db data in Cloud Functions for Firebase

In Cloud Functions for Firebase, for example:

exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
    .onWrite(event => {
    //how to access data at another node, for example 
    //important/messages/{pushId}
})

How to do I read data at another node, for example /important/messages/{pushId}? Thanks

like image 893
user3240644 Avatar asked Apr 27 '17 06:04

user3240644


People also ask

How Firebase Cloud Functions work?

Cloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your JavaScript or TypeScript code is stored in Google's cloud and runs in a managed environment.

What is Trigger in Firebase?

In a typical lifecycle, a Firebase Realtime Database function does the following: Waits for changes to a particular database location. Triggers when an event occurs and performs its tasks. Receives a data object that contains a snapshot of the data stored in the specified document.


1 Answers

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.makeUppercase = functions.database.ref('/messages/{pushId}/original').onWrite(event => {
   const getSomethingPromise = admin.database().ref(`/important/messages/{pushId}`).once('value');
   return getSomethingPromise.then(results => {
            const somethingSnapshot = results[0];
            // Do something with the snapshot
        })
    })

Check this example for instance: https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js

like image 177
Incinerator Avatar answered Oct 31 '22 05:10

Incinerator