Brian Ivander T. P.
Brian Ivander T. P.

Reputation: 485

InvalidPipeArgument:'[object Object]' for pipe'Asyncpipe'

I know this exact same question has been asked on: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe' / angularfire2 [duplicate] But following the suggestion doesn't solve my problem.

I am trying to get a list from Firebase Database and display it in ionic.

I have been able to get the data from the Database (by console.log), however, I am unable to display it in the application. (perhaps the problem is more on the *ngFor or the HTML part)

Here is the package.json in my project:

{
  "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"
}

Here is my menu.ts

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

And here is my 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>

I am still new to ionic/angular sorry if there is an obvious answer to this question.

Thanks a lot for your help.

Upvotes: 1

Views: 9395

Answers (2)

Piyush Jain
Piyush Jain

Reputation: 147

You need to parse your data once it is received using JSON.parse() or using | json

In your case it can be something like:-

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

(or)

in html

<ion-item *ngFor="let data of menuData | async">
      <h2> {{data | json }}</h2>
</ion-item>

Upvotes: 0

Sunil
Sunil

Reputation: 11243

Since you storing the array, you don't need to use async. async is used when you are trying to get the value through 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>

If you want to keep the async then change the following line

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

to

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

Upvotes: 5

Related Questions