【问题标题】:How can I add field in a certain collection? [closed]如何在某个集合中添加字段? [关闭]
【发布时间】:2021-03-07 07:22:34
【问题描述】:

我想知道如何在某个用户 uid 中添加字段

所以我想为当前用户“Rowan Whitethorn”添加另一个字段,例如地址。我该怎么做?

目前,我可以添加另一个字段,但它会创建另一个文档。

const userRef = firestore.collection('users').doc(currentUser.uid);

async function addAddress() {
   const res = await userRef.set({
       'Address' : 'Ayala'
   }, { merge : true});
}

【问题讨论】:

  • 一般来说,您使用要添加的字段更新记录。集合文档中不需要包含相同的字段。因此,如果您想在集合中的所有文档中使用它,则需要更新添加该字段的所有文档

标签: reactjs firebase google-cloud-firestore


【解决方案1】:

Firestore 文档描述了“合并”功能,您通常会使用“.set”创建一个新文档,然后传递一个参数“合并”,告诉 Firebase 不要覆盖您的文档数据,在您的情况下它会是这样的:

const userRef = db.collection('users').doc(doc_id);

const res = await userRef.set({
    'some_new_field': 'value'
}, { merge: true });

这是他们文档的链接,你会在“合并”下找到一个类似的例子:https://firebase.google.com/docs/firestore/manage-data/add-data

【讨论】:

  • 所以在firestore中,它会在该uid中添加另一个字段,如地址,没有问题,对吧?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-23
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
  • 2015-09-06
相关资源
最近更新 更多