Accessing 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 read data in another node, for example /important/messages/{pushId}? Thanks

+6
source share
1 answer
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 out this example, for example: https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js

+11
source

Source: https://habr.com/ru/post/1016811/


All Articles