【问题标题】:Access db data in Cloud Functions for Firebase在 Cloud Functions for Firebase 中访问 db 数据
【发布时间】:2017-09-24 19:36:55
【问题描述】:

在 Cloud Functions for Firebase 中,例如:

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}?谢谢

【问题讨论】:

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


    【解决方案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
            })
        })
    

    例如检查这个例子:https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js

    【讨论】:

    • 如何读取节点/messages/{pushId}/original! 上的剩余数据?例如,如果您想读取路径上的剩余数据 /messages/{pushId}/
    • event.data.val() 将为您提供/messages/{pushId}/original 节点中的数据
    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 2018-05-31
    • 2018-11-23
    • 2017-10-17
    相关资源
    最近更新 更多