【问题标题】:refer doc id using where condition in snapshotChanges()在 snapshotChanges() 中使用 where 条件引用 doc id
【发布时间】:2020-08-16 16:00:53
【问题描述】:

我正在使用snapshotChanges() 获取数据。我想向它添加where 条件并从collection 中仅获取1 个文档 的数据。

下面是我的代码:

this.firestore.collection('notifications' , ref => ref 
 .where(document id , "==" , "1")  // giving error
)
.snapshotChanges()
.subscribe(response => {
  if(!response.length){
    console.log("no data available");
    return false;
  }
  this.notifyarr = [];   
  for(let item of response){
    this.notifyarr.push(item.payload.doc.data());
    console.log(this.notifyarr);
    this.notification_length = this.notifyarr.length;
    console.log(this.notification_length);
  }
}) 

这行代码报错.where(document , "==" , "1") // giving error

我们如何使用.snapshotChanges() 引用文档?

【问题讨论】:

  • 你遇到了什么错误..!?
  • document 不是正确的词,我想知道如何引用doc,写什么来代替document ??
  • 您引用文档的方式正确,您的引用没有问题。你得到什么错误..?!
  • nope - 错误 - 'Document' 类型的参数不可分配给'string | 类型的参数场路径”。类型“文档”不可分配给类型“字符串”。
  • 尝试使用 ` .where(ref.id , "==" , "1") ` 但它显示 `没有可用数据

标签: firebase google-cloud-firestore angularfire


【解决方案1】:

如果您想查询以检查 documentid 是否等于该值,那么您应该执行以下操作:

where(firebase.firestore.FieldPath.documentId(), "==" , "1")

【讨论】:

  • 我试过i used firebase.firestore().collection("notifications").doc(this.loggedInuser) .onSnapshot(doc=> {` ,谢谢你的回答。有什么区别在我获得新条目时,在 snapshotChanges 和 onSnapshot 之间
  • onSnapshot() 用于 firebase javascript sdk,而 snapshotChanges() 用于 angularfire 库,如果您使用 angular,请使用 angularfire 库
  • 我同时使用两者,但由于我使用 ionic,从未来我将坚持使用 snapshotChanges()
【解决方案2】:

如果您知道您只需要使用其 ID 的单个文档,为什么不直接为所需文档构建一个 AngularFirstoreDocument 并在该引用上调用 snapshotChanges

this.firestore.collection('notifications').doc(id).snapshotChanges()...

无需费心构建只为您提供单个文档的查询。

【讨论】:

  • 我用了`firebase.firestore().collection("notifications").doc(this.loggedInuser).onSnapshot(doc=> {`,谢谢你的回答。@和有什么区别987654324@ 和 onSnapshot 我收到新条目
  • 我链接到的文档描述了获取数据的各种方法的行为。我建议使用它作为参考。
猜你喜欢
  • 1970-01-01
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
  • 1970-01-01
  • 2012-12-11
  • 1970-01-01
相关资源
最近更新 更多