[英]Property 'then' does not exist on type Observable<QuerySnapshot<DocumentData>>
db.collection("cities").get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
I'm trying to read multiple document of a collection, but getting error on then.我正在尝试读取集合的多个文档,但随后出现错误。 Please help me to sort out this.
请帮我解决这个问题。
If you want to treat is as a promise, you can convert it to a promise.如果要将其视为 promise,则可以将其转换为 promise。 This isn't a hack, once there's only one emission being produced by
get()
.一旦
get()
只产生一个发射,这不是黑客攻击。 But Firebase expected you to deal with the stream that's coming out from get()
though.但是 Firebase 希望你处理从
get()
出来的 stream 。 Anyway, to convert it to a promise:无论如何,要将其转换为 promise:
db.collection("cities").get().toPromise()
.then((querySnapshot: QuerySnapshot<DocumentData>) => {
querySnapshot.forEach((doc: any) => {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
If you decide to deal with the streaming coming out from get()
, as Firebase API expected you to:如果您决定处理来自
get()
的流,因为 Firebase API 期望您:
db.collection("cities").get()
.subscribe((querySnapshot: QuerySnapshot<DocumentData>) => {
querySnapshot.forEach((doc: any) => {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
[UPDATE] 1: Based on the docs, there's a new API for getting the snapshots, using the onSnapshot
method: [更新] 1:基于文档,有一个新的 API 用于获取快照,使用
onSnapshot
方法:
db.collection("cities")
.onSnapshot((querySnapshot: QuerySnapshot<DocumentData>) => {
querySnapshot.forEach((doc: any) => {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.