You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular Firestore 查询引用指定collection_b文档的collection_a文档报错怎么解决

问题根因

你代码中获取的refB是AngularFire封装的AngularFirestoreDocument实例,并非Firestore SDK原生要求的DocumentReference对象,Firestore查询逻辑无法识别该封装类实例,因此抛出字段类型不支持的错误。控制台打印看起来正常是因为封装对象内部挂载了原生引用的属性,打印时会输出内部结构,不代表类型符合要求。

修复代码

调用AngularFirestoreDocument实例的ref属性即可获取原生文档引用对象,修改后代码如下:

getAsFromB(id) {
      // 追加.ref获取原生DocumentReference实例
      var refB = this.firestore.collection("/collection_b").doc(id).ref;
      console.log(refB);

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

补充排查

如果修改后仍无查询结果,请确认collection_a的refB字段存储的是Firestore原生DocumentReference类型:

  • 如果字段实际存储的是collection_b的文档ID字符串,将查询条件改为ref.where("refB", "==", id)即可
  • 如果是跨Firestore实例的查询,还需要确保两个引用所属的Firestore实例一致

内容的提问来源于stack exchange,提问作者Schmetti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 12:18:05