简体   繁体   English

Observable 类型上不存在属性“then” <querysnapshot<documentdata> &gt; </querysnapshot<documentdata>

[英]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.

相关问题 'Observable' 类型上不存在属性 'then' <documentsnapshot<documentdata> > Angular Firebase </documentsnapshot<documentdata> - Property 'then' does not exist on type 'Observable<DocumentSnapshot<DocumentData>> Angular Firebase 类型 &#39;() =&gt; DocumentData&#39; 上不存在属性 &#39;data&#39; - Property 'data' does not exist on type '() => DocumentData' 类型“CollectionReference”上不存在属性“添加”<documentdata> '</documentdata> - Property 'add' does not exist on type 'CollectionReference<DocumentData>' “DocumentReference”类型上不存在属性“更新”<DocumentData> &#39; 打字稿 - Property 'update' does not exist on type 'DocumentReference<DocumentData>' Typescript 属性&#39;mergeMap&#39;在类型&#39;Observable上不存在 <any> “ - Property 'mergeMap' does not exist on type 'Observable<any>' Observable 类型上不存在属性管道 - Property pipe does not exist on type Observable &#39;Observable&#39; 类型不存在属性 &#39;catch&#39;<any> &#39; - Property 'catch' does not exist on type 'Observable<any>' “Observable”类型上不存在属性“过滤器”<Event> &#39; - Property 'filter' does not exist on type 'Observable<Event>' 错误:类型&#39;OperatorFunction &lt;{},{} |属性&#39;subscribe&#39;不存在 可观察的 <any> &gt; - Error: property 'subscribe' does not exist on type 'OperatorFunction<{}, {} | Observable<any>> Angular 6:“typeof Observable”类型上不存在“fromEvent”属性 - Angular 6 : Property 'fromEvent' does not exist on type 'typeof Observable'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM