Ionic 3,Firebase和angularfire2错误

时间:2018-07-22 02:37:06

标签: firebase ionic3 angularfire2

我收到以下错误消息:

ERROR Error: Uncaught (in promise): Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'
Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'

当我尝试从Firebase数据库中列出数据时,我不知道为什么。查看我的代码,我的相关部门

"@ionic-native/core": "~4.9.2",
...
"angularfire2": "^5.0.0-rc.11",
"firebase": "^5.2.0",
"ionic-angular": "3.9.2",
"rxjs": "^6.2.2",
"rxjs-compat": "^6.2.2",

我的服务

import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { Item } from './../../models/item/item.model';

@Injectable()
export class ShoppingListService {

  private shoppingListRef;
  constructor(private db: AngularFireDatabase) {
    this.shoppingListRef = this.db.list<Item>('shopping-list');
  }

  getShoppingList() {
    return this.shoppingListRef;
  }

}

我的组件

...
export class HomePage implements OnInit {

  shoppingList$: Observable<Item[]>

  constructor(
    public navCtrl: NavController,
    private shopping: ShoppingListService
  ) {}

  ngOnInit() {
    this.shoppingList$ = this.shopping
    .getShoppingList()
    .snapshotChanges()
    .subscribe(changes => {
      return changes.map(change => {
        let retorno = {
          key: change.payload.key,
          ...change.payload.val()
        };
        return retorno;
      });
    });
  }
...

最后是我的模板

<ion-item *ngFor="let item of shoppingList$ | async">
   {{item.name}}
</ion-item>

如果我删除async管道,则会遇到相同的错误。我已经尝试过这个问题,但没有成功: Angular 4: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'

知道我在做什么错吗?预先感谢。

1 个答案:

答案 0 :(得分:0)

这是从angularfire2检索数据的旧方法,现在我可以使用它:

在我的组件中:

...
export class HomePage implements OnInit {

  shoppingList$: Observable<Item[]>

  constructor(
    public navCtrl: NavController,
    private shopping: ShoppingListService
  ) {}

  ngOnInit() {
    this.shoppingList$ = this.shopping
    .getShoppingList()
    .valueChanges();
  }
...

一切正常。