从AngularFire获取数据时出错(Error getting data from AngularFire)

编程入门 行业动态 更新时间:2024-10-16 04:29:50
从AngularFire获取数据时出错(Error getting data from AngularFire)

我正在尝试将数据设置到我的表中。 我有一个应该检索这样的数据的服务:

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); }); }

更多推荐

本文发布于:2023-08-06 07:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446602.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   AngularFire   data   Error

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!