访问Firebase云端函数中的数据库数据 [英] Access db data in Cloud Functions for Firebase

查看:109
本文介绍了访问Firebase云端函数中的数据库数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

  exports.makeUppercase = functions.database.ref('/ messages / {pushId} / original')
.onWrite(event => {
//如何访问另一个节点上的数据,例如
// important / messages / {pushId}




$ p $如何在另一个节点读取数据,例如 /重要/消息/ {pushId} ?谢谢

解决方案

  const functions = require('firebase-functions'); 
const admin = require('firebase-admin');
admin.initializeApp(functions.config()。firebase);
$ b $ exports.makeUppercase = functions.database.ref('/ messages / {pushId} / original')
.onWrite(event => {
const getSomethingPromise = admin。数据库()。ref(`/ important / messages / {pushId}`).once('value');

return getSomethingPromise.then(results => {
const somethingSnapshot =结果[0];

//使用快照
})
})

请查看以下示例: https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js


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

解决方案

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

这篇关于访问Firebase云端函数中的数据库数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆