访问Firebase的Cloud Functions中的数据库数据

时间:2017-04-27 06:47:19

标签: firebase firebase-realtime-database google-cloud-functions firebase-cloud-functions

在Firebase的Cloud Functions中,例如:

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

如何在其他节点上读取数据,例如/important/messages/{pushId}?感谢

1 个答案:

答案 0 :(得分:18)

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
    })
})

例如,检查此示例:https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js