在 snapshotChanges() 中使用 where 条件引用文档 ID
refer doc id using where condition in snapshotChanges()
我正在使用 snapshotChanges()
获取数据。我想向它添加 where
条件并从 collection
.
中仅获取 1 doc 的数据
下面是我的代码:
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()
引用文档?
如果要查询以检查 documentid
是否等于该值,则应执行以下操作:
where(firebase.firestore.FieldPath.documentId(), "==" , "1")
如果您知道您只想使用其 ID 的单个文档,为什么不在该引用上构建一个 AngularFirstoreDocument to the desired document and call snapshotChanges?
this.firestore.collection('notifications').doc(id).snapshotChanges()...
无需费心构建只为您提供单个文档的查询。
我正在使用 snapshotChanges()
获取数据。我想向它添加 where
条件并从 collection
.
下面是我的代码:
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()
引用文档?
如果要查询以检查 documentid
是否等于该值,则应执行以下操作:
where(firebase.firestore.FieldPath.documentId(), "==" , "1")
如果您知道您只想使用其 ID 的单个文档,为什么不在该引用上构建一个 AngularFirstoreDocument to the desired document and call snapshotChanges?
this.firestore.collection('notifications').doc(id).snapshotChanges()...
无需费心构建只为您提供单个文档的查询。