【问题标题】:Angular Firestore - Search for all documents with reference to specifig documentAngular Firestore - 参考特定文档搜索所有文档
【发布时间】:2021-08-27 11:04:59
【问题描述】:

我正在尝试搜索集合 a 中的所有文档,并引用集合 b 中的特定文档。

我在这里或在谷歌上找到的所有东西都不起作用:/

我在服务类中的尝试

  getAsFromB(id) {
      var refB = this.firestore.collection("/collection_b").doc(id);
      console.log(refB);

      return this.firestore
        .collection("/collection_a", (ref) => ref.where("refB", "==", refB))
        .snapshotChanges();
  }

我得到的只是以下错误:不支持的字段值:refB = custom Object 但日志说 refB 看起来不错 AngularFirestoreDocument

我忘记了什么或者我做错了什么?

提前多谢

【问题讨论】:

    标签: angular typescript google-cloud-firestore


    【解决方案1】:

    您在这里的第一个查询:

    var refB = this.firestore.collection("/collection_b").doc(id);
    

    指向 Firestore 文档,但您需要指定要引用它。要修复它,将该行更改为:

    const refB = this.firestore.collection("/collection_b").doc(id).ref;
    

    这是您的函数的修改版本,它将注销您的查询结果以验证它是否返回了预期的文档:

    getAsFromB(id: string) {
       const refB = this.firestore.collection("/collection_b").doc(id).ref;
       console.log(refB);
    
       return this.firestore
         .collection("/collection_a", (ref) => ref.where("refB", "==", refB))
         .snapshotChanges().subscribe(res => {
           res.forEach(doc => {
             console.log(doc.payload.doc.data());
           })
         });
     }
    

    由于这是在服务中,您需要删除订阅,我只是认为它可能对您的调试有用:)

    PS 在 Typescript 中使用 const 和 let 代替 var 也是一种很好的做法。

    【讨论】:

    • 太棒了,谢谢!我以为我试过了,但我查看了我的浏览器历史记录,它还说在 where 子句中添加“.ref”,这就是它没有工作的原因:(ref) => ref.where("refB", "==", refB.ref)
    猜你喜欢
    • 1970-01-01
    • 2018-04-06
    • 2021-03-25
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 2018-06-16
    相关资源
    最近更新 更多