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