Access db data in Cloud Functions for Firebase

2019-01-09 12:49发布

问题:

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

回答1:

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