简体   繁体   English

从AngularFire获取数据时出错

[英]Error getting data from AngularFire

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(…) zone.js:357错误:未捕获(承诺):错误:./SmartTables类SmartTables_Host中的错误-内联模板:0:0,其原因是:this.service.getData(...)。然后不是函数(… )

AngularFire2 works with Observables, which you subscribe to. AngularFire2与您subscribe Observables一起使用。 The error you are getting is because you are trying to call an undefined function then , which is available in Promises, not Observables. 您收到的错误是因为您尝试调用一个未定义的函数then ,该函数在Promises(而非Observable)中可用。

You want to replace then with subscribe to retrieve the data. then ,您要替换为subscribe以检索数据。

constructor(protected service: SmartTablesService) {
   this.service.getData().subscribe((data) => {
     this.source.load(data);
   });
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM