【问题标题】:Pushing an array of objects into Firebase Collection Angular 8将一组对象推送到 Firebase Collection Angular 8
【发布时间】:2020-08-29 11:28:01
【问题描述】:

我正在尝试将文档添加到我的用户集合中的数组 studyList 中。

所以我有一个集合用户,其中我有名字等.. 和 studyList。

当我点击购买 DocumentItemComponent 的按钮时,我想将该文档添加到这个 studyList 数组中。

我的代码部分工作是因为它将文档添加到数组中,但是当我单击另一个文档时,它会更改第一个文档,它不会添加另一个文档。

这是我添加函数的代码:


   addToStudyList(user) {
        const userRef: AngularFirestoreDocument<any> = this.afs.doc(`users/${user.id}`);
        const data: UserInterface = {
          studyList: [{
            title: this.document.title,
            language: this.document.language,
            description: this.document.description,
            cover: this.document.cover,
            category: this.document.category,
            field: this.document.field,
            id: this.document.id,
            author: this.document.userUid,
            urlDocument: this.document.urlDocument
          }]

        }
        return userRef.set(data, {merge: true});

   }

你能帮帮我吗? 谢谢!祝你有美好的一天!

【问题讨论】:

    标签: arrays angular firebase collections push


    【解决方案1】:

    这是因为当你声明:

      studyList: [{
                title: this.document.title,
                language: this.document.language,
                description: this.document.description,
                cover: this.document.cover,
                category: this.document.category,
                field: this.document.field,
                id: this.document.id,
                author: this.document.userUid,
                urlDocument: this.document.urlDocument
              }]
    

    在这段代码中,您只为 studyList 数组分配了一个对象,该数组覆盖了现有数组,而您应该利用现有的用户 studyList 数组并将新对象推入其中,如下所示:

      addToStudyList(user) {
        const userRef: AngularFirestoreDocument<any> = this.afs.doc(`users/${user.id}`);
        user.studyList.push({
            title: this.document.title,
            language: this.document.language,
            description: this.document.description,
            cover: this.document.cover,
            category: this.document.category,
            field: this.document.field,
            id: this.document.id,
            author: this.document.userUid,
            urlDocument: this.document.urlDocument
          });
        const data: UserInterface = {
          studyList: user.studyList
    
        }
        return userRef.update(data);
    

    }

    【讨论】:

    • 我试过这种方式@ploofah,但现在在firebase中它是''studyList:2''。我认为这需要文件的数量。
    • 我已经更新了上面的数据对象。这应该可以解决 2 被保存的问题。 updatedStudyList 有推送后数组的长度。
    • 是的,对不起.. 我刚刚意识到,在版主批准之前,这些编辑内容将不可见。 :) 只需使用上面的代码行。
    • 完美运行!!太感谢了!对此,我真的非常感激!你是英雄。
    【解决方案2】:

    没有直接的方法来更新文档中的数组,但是如果您使用 Firestore,它提供了 arrayUnion 和 arrayRemove 函数,您可以使用它们来添加/删除数组中的唯一项。

    来自 Firestore 文档 https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array:

    试试这个:

    userRef.update({
      studyList: firebase.firestore.FieldValue.arrayUnion(data)
    });
    

    【讨论】:

    • 是的,我正在使用 Firestore,我阅读了文档,但我不知道如何使用它。我只是在构造函数中注入了firestore。我应该进口别的东西吗?这就是错误: DocumentItemComponent.html:62 ERROR TypeError: Cannot read property 'fieldValue' of undefined at DocumentItemComponent.addToStudyList
    • 您在使用哪个库作为 firebase?尝试 import * as firebase from 'firebase/app'
    • 我试过这个,但它不起作用,我不知道为什么。无论如何,我用 Nayak 的回答解决了我的问题,非常感谢!
    • 我是同一个人 :) 通常我不喜欢每次都通过网络发送大量数据,所以试图看看这是否有效,那么你可能只能发送一个项目,而不是每次进行添加/编辑时都发送所有项目。无论如何,如果发送所有数据都适合您的情况,那么您就可以开始了。谢谢!
    • Ups,我没看名字!对不起
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    相关资源
    最近更新 更多