Firebase实时数据库在使用angularfire 2进行过滤后更新条目

时间:2018-09-18 06:07:22

标签: firebase firebase-realtime-database angularfire2

实时数据库中的数据如下所示:

"orders" : {
    "-LM5p4tl7Og_PMJOspZ-" : {
      "customer" : {
        "_name" : "Vik Kumar",
        "_uid" : "oSEYj0zrkhPCk9r7uwyOOkHcqe53"
      },
      "order" : {
        "_orderNumber" : "VikKumar-26954",
        "_orderStatus" : "Pending",
        "_orderTotal" : 500,
      }
    },
    "-LM5s1wRCvqWA1vsZONy" : {
      "customer" : {
        "_name" : "Vik Kumar",
        "_uid" : "oSEYj0zrkhPCk9r7uwyOOkHcqe53"
      },
      "order" : {
        "_orderNumber" : "VikKumar-11423",
        "_orderStatus" : "Pending",
        "_orderTotal" : 500,
      }
    },
    "-LM5sOHzjLeqFGFleMxq" : {
      "customer" : {
        "_name" : "Vik Kumar",
        "_uid" : "oSEYj0zrkhPCk9r7uwyOOkHcqe53"
      },
      "order" : {
        "_orderNumber" : "VikKumar-63772",
        "_orderStatus" : "Pending",
        "_orderTotal" : 500,
      }
    }
}

我在我的离子应用程序中使用angularfire2,并过滤了以下一些订单:

getPendingOrders(dateStr:string){
    return new Promise((resolve, reject) =>
      {
        this.db.list('orders',
                     ref =>  ref.orderByChild("order/_date").equalTo(dateStr)
       ).valueChanges().subscribe(
        res => {
          resolve(res)
        },
        err => {
          console.log(err)
          reject(err)
        }
      )
      })
  }

上面的查询给了我如下响应

[{"customer": {}, "order": {}}, {"customer": {}, "order": {}}]

我可以在UI中很好地渲染它,我希望最终用户能够更新属性“ _orderStatus”的值。问题是我查询丢失了识别特定对象以更新它的键的数据的方式。请指教

1 个答案:

答案 0 :(得分:1)

好的,不确定是否有帮助,我认为您已经迁移到使用AngularFire 5.0。调用.valueChanges()将返回一个没有任何元数据的Observable(例如,项目键),您需要改用.snapshotChanges()调用:

this.db.list(...)
    .snapshotChanges()
    .map(changes => {
      return changes.map(change => ({key: change.payload.key, ...change.payload.val()}));
    })
    .subscribe(...)