【问题标题】:Angularfire2 update all children nodesAngularfire2 更新所有子节点
【发布时间】:2017-08-03 12:28:24
【问题描述】:

在 Firebase 中,我有这个数据库

miodb

我想更新所有子节点(例如 67-68 的子节点),将键 'status' 设置为 'read'。

这就是我获取项目列表的方式

this.listaquery = db.list('https://xxxxxxxx.firebaseio.com/chat/'+this.navParams .get('chatRoom'),{
          query: {
              orderByChild: 'id',
              equalTo: this.navParams .get('id2')
          }
        });
      }

我知道这是更新孩子的代码

this.listaquery.update('-KnFI98Lnm-BYXxR_zoY',{status:'read'});

但是如果我想用一个命令更新所有的孩子呢?

【问题讨论】:

    标签: javascript firebase firebase-realtime-database angularfire2


    【解决方案1】:

    我找到了这个解决方案(不知道是不是最好的)

    this.listaquery = db.list('https://delega-facile.firebaseio.com/chat/'+this.navParams .get('chatRoom'),{
      query: {
          orderByChild: 'id',
          equalTo: this.navParams .get('id1')
      },
      preserveSnapshot: true
    });
    
    
    this.listaquery
              .subscribe(snapshots => {
                  snapshots.forEach(snapshot => {
                      this.listaquery.update(snapshot.key,{status :'read'});
                      /*
                      console.log(snapshot.key)
                      console.log(snapshot.val())
                      */
                  });
              })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多