Firestore:如何更新文档的特定字段? [英] Firestore: How to update specific field of document?

查看:107
本文介绍了Firestore:如何更新文档的特定字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问和更新角度Firestore中的特定字段:

How do I access and update a specific field in angular firestore:

推荐答案

好,您必须执行以下步骤:

Ok you have to do the following steps:

  • 首先请确保您创建的查询名称或ID或什至两者都必须是唯一的
  • 然后您通过snapshotChanges订阅此查询
  • 接下来,您将从所查询的对象中获取ID
  • 此后,您将使用此ID使用新值更新文档

它看起来像这样:

updateDoc(_id: string, _value: string) {
  let doc = this.afs.collection('options', ref => ref.where('id', '==', _id));
  doc.snapshotChanges().pipe(
    map(actions => actions.map(a => {                                                      
      const data = a.payload.doc.data();
      const id = a.payload.doc.id;
      return { id, ...data };
    }))).subscribe((_doc: any) => {
     let id = _doc[0].payload.doc.id; //first result of query [0]
     this.afs.doc(`options/${id}`).update({rating: _value});
    })
}

这篇关于Firestore:如何更新文档的特定字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆