【问题标题】:How to add an object to an array + Firestore?如何将对象添加到数组+ Firestore?
【发布时间】:2018-08-16 18:00:55
【问题描述】:

我想将 Object 添加到 firestore 中现有的 Array。 我已经在Array 中有一个Object,现在我想添加第二个Object

所以我在下面创建了这段代码来将数据存储到 Firestore。 触发函数后,firestore 中的数据不会向数组中添加新的Object

代码:

  let docId = `${this.currentUser.uid}`

  fb.usersCollection.doc(docId).update({
      userId: this.currentUser.uid,
      posts: [
        {
          createdOn: this.postDetails.createdOn,
          content: this.postDetails.content,
          image: this.postDetails.image,
          comments: this.postDetails.comments,
          likes: this.postDetails.likes,
          tags: this.model,
          userData: [

            { userName: this.userProfile.name,
              userId: this.currentUser.uid,
              userImage: this.userProfile.userImage
            }
          ]
        }
      ]
    })

【问题讨论】:

标签: javascript firebase google-cloud-firestore


【解决方案1】:

更新不会自动将新对象添加到数组中,您这样做的方式只是用新对象覆盖 posts 数组。

要将新对象添加到现有数组,您可以读取旧数组然后自己操作它,然后使用事务方法将其写回或使用@Joseph 提到的新数组方法

【讨论】:

    猜你喜欢
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 2019-09-13
    • 2020-12-28
    • 2021-06-02
    相关资源
    最近更新 更多