【问题标题】:Firebase functions - get sibling of current DataSnapshotFirebase 函数 - 获取当前 DataSnapshot 的同级
【发布时间】:2019-02-12 16:51:24
【问题描述】:

我的数据库结构如下

我正在通过在 firebase 云功能中使用 onWrite(snapshot, context) 来收听活动节点。我想根据actives的变化来获取token节点的值。

我的触发函数如下。

exports.sendNotification = functions.database.ref('root/users/{userid}/actives/{pushId}')
.onWrite((event, context) => {

var token; 

var active_userid = context.params.pushId;
   if(event.after._data != null){ 
        console.log(event.after.ref.parent.parent);

   }else{

       console.log('data is null')

   } 
return 0;

});

如何获取token节点并读取其值?

【问题讨论】:

  • 您可以登录active_userid 并告诉我您在控制台中得到了什么吗?在var active_userid = context.params.pushId;之后写console.log(active_userid)
  • @PeterHaddad 使用关于我得到 ​​hasnain 的图像
  • 让你的 pushId 不为 null 或 ""(empty)。
  • @MayurPatel 这不是 pushId。在下面查看我的答案

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


【解决方案1】:

我解决了

 var ref = event.after.ref.parent.parent.child('token');

          ref.on('value', function(snapshot) {
                        console.log(`token value : ${snapshot.val()}`);
                        });

【讨论】:

  • 仅供参考,在云函数中最好使用once() 而不是on()
  • @RenaudTarnec 怎么样?
  • var ref = event.after.ref.parent.parent.child('token'); return ref.once('value').then(function(snapshot) {});firebase.google.com/docs/reference/node/…
  • 知道了。谢谢@RenaudTarnec
猜你喜欢
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-19
  • 1970-01-01
  • 2020-10-15
  • 1970-01-01
相关资源
最近更新 更多