InvalidPipeArgument:“ [object Object]”表示管道“ Asyncpipe”

时间:2018-10-12 18:07:24

标签: angular firebase ionic-framework

我知道有人问过同样的问题: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe' / angularfire2 [duplicate] 但是遵循建议并不能解决我的问题。

我正试图从Firebase数据库中获取一个列表,并将其显示在ionic中。

我已经能够(通过console.log从数据库中获取数据,但是,我无法在应用程序中显示。 (也许问题更多出在* ngFor或HTML部分)

这是我项目中的package.json:

{
  "name": "brianAppResto",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "http://ionicframework.com/",
  "private": true,
  "scripts": {
    "start": "ionic-app-scripts serve",
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "lint": "ionic-app-scripts lint"
  },
  "dependencies": {
    "@angular/animations": "^5.1.3",
    "@angular/common": "^5.1.3",
    "@angular/compiler": "^5.1.3",
    "@angular/compiler-cli": "^5.1.3",
    "@angular/core": "^5.1.3",
    "@angular/fire": "^5.0.2",
    "@angular/forms": "^5.1.3",
    "@angular/http": "^5.1.3",
    "@angular/platform-browser": "^5.1.3",
    "@angular/platform-browser-dynamic": "^5.1.3",
    "@angular/platform-server": "^5.1.3",
    "@angular/router": "^5.1.3",
    "@ionic-native/core": "~4.15.0",
    "@ionic-native/splash-screen": "~4.15.0",
    "@ionic-native/status-bar": "~4.15.0",
    "@ionic/pro": "2.0.3",
    "@ionic/storage": "2.2.0",
    "firebase": "^5.5.3",
    "ionic-angular": "3.9.2",
    "ionicons": "3.0.0",
    "promise-polyfill": "^8.1.0",
    "rxjs": "^6.3.3",
    "rxjs-compat": "^6.3.3",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.26"
  },
  "devDependencies": {
    "@ionic/app-scripts": "3.2.0",
    "typescript": "~2.6.2"
  },
  "description": "An Ionic project"
}

这是我的菜单

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController } from 'ionic-angular';
import { AddMenuPage } from '../add-menu/add-menu';
import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFireDatabase, AngularFireList } from '@angular/fire/database';
import { Menu } from './../../models/menu';

/**
 * Generated class for the MenuPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-menu',
  templateUrl: 'menu.html',
})
export class MenuPage {

  menuData: AngularFireList<Menu[]>;

  constructor(private afAuth: AngularFireAuth, private afDatabase: AngularFireDatabase,
    private toast: ToastController,
    public navCtrl: NavController, public navParams: NavParams) {
  }

  ionViewDidLoad() {
    this.afAuth.authState.subscribe(data => {
      if(data && data.email && data.uid){
        this.toast.create({
          message: `Menu berhasil ditambahkan, ${data.email}`,
          duration: 3000
        }).present();

        this.menuData = this.afDatabase.list<Menu>(`menu/${data.uid}`).valueChanges().subscribe(console.log);

      }
      else {
        this.toast.create({
          message: `Could not find authentication details`,
          duration: 3000
        }).present();
      }
    });
  }

  addMenu() {
    this.navCtrl.push('AddMenuPage');
  }
}

这是我的 menu.html

<ion-header>

  <ion-navbar>
    <button ion-button menuToggle>
      <ion-icon name="menu"></ion-icon>
    </button>
    <ion-title>Menu</ion-title>
  </ion-navbar>

</ion-header>


<ion-content padding>
  <button ion-button (click)="addMenu()">Tambah Menu</button>

  <ion-list>
    <ion-item *ngFor="let data of menuData | async">
      <h2>Nama Menu: {{data.menuName}}</h2>
      <h3>Harga: {{data.menuPrice}}</h3>
    </ion-item>
  </ion-list>

</ion-content>

如果对这个问题有一个明显的答案,我仍然对离子/角度对不起。

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

由于存储了array,所以不需要使用异步。尝试通过async获取值时使用Observable

  <ion-list>
    <ion-item *ngFor="let data of menuData"> <!-- async is removed -->
      <h2>Nama Menu: {{data.menuName}}</h2>
      <h3>Harga: {{data.menuPrice}}</h3>
    </ion-item>
  </ion-list>

如果要保持异步,请更改以下行

this.menuData = this.afDatabase.list<Menu>(`menu/${data.uid}`).valueChanges().subscribe(console.log);

this.menuData = this.afDatabase.list<Menu>(`menu/${data.uid}`).valueChanges();

答案 1 :(得分:0)

一旦使用JSON.parse()或使用|来接收数据,就需要解析数据。 json

在您的情况下,可能是这样的:-

[
{
    "id": "223334455",
    "name": "lonco",
    "description": "lenco",
    "price": 83223
}
]

(或)

以html

this.menuData = JSON.parse(this.afDatabase.list<Menu>(`menu/${data.uid}`).valueChanges());