【问题标题】:Updating object in firebase using angular使用角度更新firebase中的对象
【发布时间】:2023-03-03 06:39:20
【问题描述】:

我正在使用 Angular Firebase 应用程序,一个客户更新了一些商品的购买请求,在 firebase 中使用以下方法...

 addPurchaseRequest(data, uid: string) {

 this.db.database.ref("/purchaserequests/").push(data);

 }

我在 firebase 购买了以下 2 次商品

purchaserequests
  -LXZVHEpbxfpJzt_el10
      actualbonus: 12312,
      uid:1,
      status:,
      approvedon:''
  -LXFSADSpbxfpJzt_el11
      actualbonus: 12,
      uid:2,
      status:0,
      approvedon:''

现在我想更新 uid = 1 的状态和日期

 updatepurchaserequest(rid : string , amount : number , reason: string) {
  const dataObj = {
  status: 1
    approvedon: new Date().toLocaleDateString()
   };
  this.db.database.ref("/purchaserequests/").set(dataObj);

但这不起作用并替换了完整的购买请求对象。

我需要 Mysql 的 where 子句之类的东西,它说 update dataObj where uid =1

【问题讨论】:

  • Firebase 没有更新查询的概念。您必须执行查询 (orderByChild("uid").equalTo(1)),然后循环遍历结果并分别在每个匹配节点上调用更新。
  • 谢谢,我可以放 2 where 子句 ..like update where uid = 1 and where approvedon = '' 吗?提前致谢
  • Firebase 查询只能包含一个条件。见stackoverflow.com/questions/26700924/…

标签: angularjs firebase firebase-realtime-database


【解决方案1】:

我认为您可能必须通过 $key 进行编辑或使您的 uid 像您的 $key 一样

当您获取数据时,您可以这样做:

yourData.forEach( u => {
  const _data = u.payload.toJSON();
  _data['$key'] = u.key;
  this.inactiveUsers.unshift(_data);
})

有了这个,$key 是你数据的一部分,如果你想更新它,你可以这样做:

this.db.database.ref(purchaserequests).child($key).update(item);

而且,如果您想更新一个或多个参数:

this.db.database.ref(`purchaserequests/${$key}`).update({ [atribute]: value });

【讨论】:

  • 谢谢,但是密钥需要是唯一的,因为一个 uid 可以添加许多购买请求
  • 好的!但是我说的关键是firebase放的关键,所以它是独一无二的。另一方面,你可以把你的 uid 像你的对象的 $key 一样。 this.db.database.ref(purchaserequests/{yourUID}).set(item);
猜你喜欢
  • 2019-06-26
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 2020-12-22
  • 2015-11-23
  • 2013-09-28
  • 2018-02-02
  • 1970-01-01
相关资源
最近更新 更多