【问题标题】:firestore collection, retrieving field values to store and usefirestore 集合,检索要存储和使用的字段值
【发布时间】:2021-07-13 00:49:09
【问题描述】:

我正在尝试访问我的 Firestore 集合并遍历所有文档以提取每个文档中的 valenceId 字段。

我尝试了以下操作,但显示错误“无法检索未定义的 valenceId”。我也尝试过使用 observable 但无法提取数据。这对我来说是新的,所以任何建议都将不胜感激。

我试过的代码:

doc:Item;
doc_comments: Array<number>;

ngOnInit() {
  this.db.collection('entry').doc<Item> 
     ('doc_Id').valueChanges().subscribe( data =>{
        this.doc=data;
        this.doc_comments= Array(data.Ids);
        console.log('doc_comments', this.doc_comments);
      }
   );
}

【问题讨论】:

    标签: angular google-cloud-firestore ionic4


    【解决方案1】:

    valueChangessubscribe 调用之后,您将获得一个包含您要收集的数据的对象数组,因此我在内部使用forEach 对其进行迭代,它对我有用。

    请注意,this.doc 在您的示例中不是一个数组,因此它将保存最后一个数组,我认为这不是您的目标。

    doc:Item;
    doc_comments: Array<number>;
    
    ngOnInit() {
      this.db.collection('entry').valueChanges().subscribe( data_array =>{
            data_array.forEach(doc => {
                this.doc = data;
                this.doc_comments = Array(data.Ids);
            })
            console.log('doc_comments', this.doc_comments);
          }
       );
    }
    

    【讨论】:

      【解决方案2】:

      您要做的是在名为“doc_Id”的单个文档上调用.valueChanges() 函数...如果您想遍历集合中的所有文档,只需使用

      this.db.collection('diaryEntry').valueChanges().subscribe(
      // your logic goes here
      )
      

      【讨论】:

        猜你喜欢
        • 2020-10-17
        • 2014-07-24
        • 2021-11-17
        • 2021-04-04
        • 2021-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多