小编典典

将侦听器添加到Firebase生成的密钥中的子项?

json

给定布局

+ activity
  + -K5p_pBXog4kb0SVLXxj
    text:'something',
    when:111
    + thread
      + -K5paF53zm3cuudP9FUQ 
        id:8,
        text:'something else'

有没有一种方法可以将侦听器添加到活动中的任何新“线程”插入中,例如/ activity / * / thread?

当添加了新的活动时,我正在获取这些活动的提要,但是在附加线程时,不会触发任何事件。我是否需要将[thread]部分移到它自己的根目录下,并将线程ID与活动ID匹配,才能知道何时针对主活动线程进行线程注释?


阅读 331

收藏
2020-07-27

共1个答案

小编典典

根据您的评论更新答案。

您想听/activity/$theid/thread/$threadId

与其嵌套,不如嵌套/thread/$threadId在根中。

{
  "threadActivity": {
     "$activityId": {
        "$threadId" : {

        } 
     }
   } 
}

现在,当添加新线程时,您可以只听/threadActivity/$activityId

var ref = new Firebase('https://<my>.firebaseio.com/threadActivity');
var activityRef = ref.child('some-id');
activityRef.on('child_added', function(snap) {
  // will update you for every child at the location and each child added
  console.log(snap.val()); 
});
2020-07-27