简体   繁体   English

Ionic&AngularFireDatabase列表结果-类型“ {}”上不存在属性“ map”

[英]Ionic & AngularFireDatabase list result - Property 'map' does not exist on type '{}'

I am trying to get data from Firebase by the list function and to use it in Json format (to load markers on my Google Map). 我正在尝试通过list功能从Firebase获取数据并以Json格式使用它(以在我的Google Map上加载标记)。 But I'm facing an issue with the returned snapshots : Property 'map' does not exist on type '{}' 但是我遇到了与返回的snapshots的问题: 属性“ map”在类型“ {}”上不存在

My imports: 我的进口:

import { Component, ViewChild, ElementRef } from '@angular/core';
import { NavController, IonicPage } from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation';
import { AngularFireDatabase } from 'angularfire2/database';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

And the code snippet with the error: 以及带有错误的代码段:

markers: Observable<any[]>;
constructor(public navCtrl: NavController, 
      public geolocation: Geolocation, 
      public db: AngularFireDatabase) { 

        this.markers = this.db.list("empresa/", {preserveSnapshot: true})
          .map(snapshots => {
            return snapshots.map(snapshot => { // this second .map displays the error
              return snapshot.val();    
            })
            .subscribe((marcas)=> {
              console.log(marcas);
            });
        });
      }

Because of this, I think, the subscribe does not display anything on the console. 因此,我认为subscribe不会在控制台上显示任何内容。 I also tried to import in some different ways the rxjs, as in this link Property 'map' does not exist on type 'Observable<Response>' , but without success. 我还尝试以不同的方式导入rxjs,因为在此链接中, 属性'map'在类型'Observable <Response>'上不存在 ,但没有成功。

Seems like a migration issue - since AF 5.0 list returns an AngularFireList<T> service, not FirebaseListObservable , you have to call an action on it to stream data into an observable collection, eg: 似乎是一个迁移问题- 由于AF 5.0 list返回的是AngularFireList<T>服务,而不是FirebaseListObservable ,因此您必须对其进行调用将数据流式传输到可观察的集合中,例如:

    const subscription = this.db.list("empresa/").snapshotChanges()
      .map(snapshots => {
        return snapshots.map(snapshot => snapshot.payload.val());
      })
      .subscribe(values => {
        console.log(values);
        this.markers = values;
      });

Note also that the subscribe is chained after the outer map , not the inner as in your example; 还要注意, subscribe链接在外部map ,而不是在您的示例中在内部之后; and this.markers are being assigned results inside the subscription, so you have to change their type from Observable<any[]> to any[] (or whatever they are, just not observable). 并且在订阅中为this.markers分配了结果,因此您必须将其类型从Observable<any[]>更改为any[] (或任何它们,只是不可观察的)。

See 3. Retrieving data as lists in the docs for details; 有关详细信息,请参见3.在文档中作为列表检索数据 there's an example at the end of the page on using the listed items inside an angular template with async pipe, where you don't need to subscribe , then you'd keep this.markers as Observable<any[]> and the rest would be: 在页面末尾有一个示例,该示例使用带有异步管道的角度模板内的列出项目,您不需要subscribe ,则将this.markers保留为Observable<any[]> ,其余的将是:

this.markers = this.db.list("empresa/").snapshotChanges()
  .map(snapshots => {
    return snapshots.map(snapshot => snapshot.payload.val());
  });

This is preferred as the async pipe manages the subscription for you (because if you are subscribing manually you should also unsubscribe ). 这是首选方法,因为异步管道为您管理预订(因为如果您手动预订,还应该unsubscribe )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 离子2:“属性&#39;号&#39;&#39;在类型&#39;typeof上不存在” - Ionic 2 : “Property 'number' does not exist on type 'typeof” Ionic 2 - 属性在类型错误上不存在 - Ionic 2 - Property does not exist on type error “EventTarget”类型上不存在属性“result”? - Property 'result' does not exist on type 'EventTarget'? 属性 'map' 在类型 '() => IterableIterator 上不存在<number> '</number> - Property 'map' does not exist on type '() => IterableIterator<number>' 属性“地图”在类型“对象”角度上不存在 - Property 'map' does not exist on type 'Object' Angular 尝试从弹出结果转换数组时,属性映射在类型Observable上不存在 - Property map does not exist on type Observable when trying to convert a Array from plucked result Ionic2错误类型“窗口”上不存在属性“ openDatabase” - Ionic2 error Property 'openDatabase' does not exist on type 'Window' 离子 AngularFirestoreCollection 属性“id”不存在类型“QueryDocumentSnapshot”<todo> '</todo> - Ionic AngularFirestoreCollection Property 'id' does not exist type 'QueryDocumentSnapshot<Todo>' Ionic 2 RC0属性&#39;tz&#39;在&#39;typeof moment&#39;类型上不存在 - Ionic 2 RC0 Property 'tz' does not exist on type 'typeof moment' 离子3中“对象”类型中不存在如何修复属性? - how to fix property does not exist on type 'Object' in ionic 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM