我正在尝试将数据设置到我的表中。 我有一个应该检索这样的数据的服务:
constructor(public af: AngularFire, userData: UserData) { this.smartTableData = af.database.list('events/' + userData.user.uid +'/contacts'); } getData(): any { return this.smartTableData; }我的组件是:
constructor(protected service: SmartTablesService) { this.service.getData().then((data) => { this.source.load(data); }); }来自控制台的错误:
zone.js:357错误:未捕获(在承诺中):错误:./SmartTables类中的错误SmartTables_Host - 内联模板:0:0由:this.service.getData(...)引起。然后不是函数(... )
I am trying to set data to my table. I have a service that should retrieve data like this:
constructor(public af: AngularFire, userData: UserData) { this.smartTableData = af.database.list('events/' + userData.user.uid +'/contacts'); } getData(): any { return this.smartTableData; }and my component is:
constructor(protected service: SmartTablesService) { this.service.getData().then((data) => { this.source.load(data); }); }my error from console:
zone.js:357 Error: Uncaught (in promise): Error: Error in ./SmartTables class SmartTables_Host - inline template:0:0 caused by: this.service.getData(...).then is not a function(…)
最满意答案
AngularFire2与您subscribe Observables一起使用。 你得到的错误是因为你试图调用一个未定义的函数,这在Promises中可用,而不是Observables。
您想要使用subscribe替换以检索数据。
constructor(protected service: SmartTablesService) { this.service.getData().subscribe((data) => { this.source.load(data); }); }AngularFire2 works with Observables, which you subscribe to. The error you are getting is because you are trying to call an undefined function then, which is available in Promises, not Observables.
You want to replace then with subscribe to retrieve the data.
constructor(protected service: SmartTablesService) { this.service.getData().subscribe((data) => { this.source.load(data); }); }更多推荐
发布评论