简体   繁体   English

无法向 Firestore 中的现有文档添加新字段

[英]Not able to add a new field to an existing document in firestore

I'm trying to add a new field(reactions) to an existing document using updateDoc method but it's not adding a new field.我正在尝试使用updateDoc方法向现有文档添加一个新字段(反应),但它没有添加一个新字段。 This is how I'm doing:这就是我正在做的事情:

      const newUserReactions = [...reactions, newReaction]
      console.log(newUserReactions)
      updateDocument(docID, { reactions: newUserReactions })

reactions field doesn't exist on the document文档中不存在reactions字段

updateDocument:更新文档:

const updateDocument = async (id, updates) => {
    dispatch({ type: "IS_PENDING" })
    try {
      const updatedDocument = await updateDoc(doc(db, c, id), updates)
      dispatchIfNotCancelled({ type: "UPDATED_DOCUMENT", payload: updatedDocument })
      return updatedDocument
    } catch (error) {
      dispatchIfNotCancelled({ type: "ERROR", payload: error })
      return null
    }
  }

There are several ways to write data to Cloud Firestore:将数据写入 Cloud Firestore 有多种方法:

  • Set the data of a document within a collection, explicitly specifying a document identifier.在集合中设置文档的数据,明确指定文档标识符。

  • Add a new document to a collection.将新文档添加到集合中。 In this case, Cloud Firestore automatically generates the document identifier.在这种情况下,Cloud Firestore 会自动生成文档标识符。

  • Create an empty document with an automatically generated identifier, and assign data to it later.使用自动生成的标识符创建一个空文档,然后将数据分配给它。

When you are trying to update a document in Cloud Firestore requires knowing its ID.当您尝试更新 Cloud Firestore 中的文档时,需要知道其 ID。 To update some fields of a document without overwriting the entire document, use the update() method:要更新文档的某些字段而不覆盖整个文档,请使用update()方法:
You can do the updates by following the below algorithmic logic.您可以按照以下算法逻辑进行更新。
1.Run a query with your conditions to determine the document ID. 1.根据您的条件运行查询以确定文档 ID。
2.Update the documents with individual updates, or with one or more batched writes[1] 2.使用单个更新或一个或多个批量写入来更新文档[1]

const docRef = db.collection('objects').doc('some-id');
 
// Update the timestamp field with the value from the server
const res = await docRef.update({
  timestamp: FieldValue.serverTimestamp()
});

Note that you only need the document ID from step 1. So you could run a query that only returns the IDs.请注意,您只需要步骤 1 中的文档 ID。因此您可以运行仅返回 ID 的查询。 This is not possible in the client-side SDKs, but can be done through the REST API and Admin SDKs as shown here [2]这在客户端 SDK 中是不可能的,但可以通过 REST API 和 Admin SDK 来完成,如此处所示 [2]

[1] https://firebase.google.com/docs/firestore/manage-data/add-data#server_timestamp [1] https://firebase.google.com/docs/firestore/manage-data/add-data#server_timestamp
[2] : How to get a list of document IDs in a collection Cloud Firestore? [2] : 如何获取 Cloud Firestore 集合中的文档 ID 列表?

Are there any specific errors other than the one specified ?除了指定的错误之外,是否还有任何特定错误? Also, please share the pre and the post screen-shot of the Firestore Data from Firebase console.另外,请分享来自 Firebase 控制台的 Firestore 数据的前后屏幕截图。
You may refer the following links for further reference:您可以参考以下链接以供进一步参考:

[3] : https://firebase.google.com/docs/firestore/manage-data/add-data#add_a_document [3]: https ://firebase.google.com/docs/firestore/manage-data/add-data#add_a_document
[4] : https://firebase.google.com/docs/firestore/manage-data/add-data#set_a_document [4]: https ://firebase.google.com/docs/firestore/manage-data/add-data#set_a_document
[5] : https://cloud.google.com/firestore/docs/samples/firestore-data-set-field#firestore_data_set_field-nodejs [6] : Can I add new fields to existing document if I know its title field value and not the id? [5]: https ://cloud.google.com/firestore/docs/samples/firestore-data-set-field#firestore_data_set_field-nodejs [6]: 如果我知道它的标题字段值,我可以向现有文档添加新字段吗而不是身份证?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM