【问题标题】:Ionic Firestore CollectionReference.doc() error [closed]Ionic Firestore CollectionReference.doc()错误[关闭]
【发布时间】:2019-12-08 13:45:28
【问题描述】:

我在所有文档的 firestore 中添加了一些数组,但显示错误

core.js:6014 ERROR FirebaseError: Function CollectionReference.doc() requires its first argument to be of type non-empty string, but it was: an array

我有一个数组,我需要将它保存在所有文档中。所以首先获取所有文档 ID,然后在所有文档中保存数组,但这里显示错误是我的代码。

getAllDocs(seen){
  this.angularFirestore.collection("HomeGroup").snapshotChanges().pipe(
    map(changes=>{
      return changes.map(a=>{
          const data = a.payload.doc.data();
          const id = a.payload.doc.id;
          return {id}
      });
    }
        )).subscribe(items=>{
          console.log(items);
          this.ids = items;

         this.angularFirestore.collection('HomeGroup').doc(this.ids).add(seen);
      })

     }

它认为问题出在 ids 上?

【问题讨论】:

    标签: angular firebase ionic-framework google-cloud-firestore


    【解决方案1】:

    items 是一个数组,当你将它存储在this.ids 中时,this.ids 将成为一个数组。您需要在数组内部进行迭代并检索id

    items.forEach((ids) => {
        console.log(ids);
        this.idValue = ids.id;
    });
    

    【讨论】:

      【解决方案2】:

      您不能将数组传递给doc() 方法,您需要传递一个字符串,如错误所示。一种方法是使用Promise.all(),如下所示。 Promise.all() 允许并行执行许多 Promise,并等待它们全部解决。

      //....
      .subscribe(items=>{
             const promises = [];
      
             items.forEach(item => {          
                  promises.push(this.angularFirestore.collection('HomeGroup').doc(item.id).set(seen));
             });
             return Promise.all(promises);
      })
      

      另外,请注意add() 不是DocumentReference 的方法。您可能想致电set()update()。如果您使用set(seen)update(seen),则应注意seenDocumentDataUpdateData 类型的对象。

      【讨论】:

      • @UmaizKhan 您好,您有时间查看建议的解决方案吗?