i need your help, i'm trying to display some datas from my firebase but it trhows me an error like InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'
.
There is my service:
import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
@Injectable()
export class MoviesService {
constructor(private db: AngularFireDatabase) {}
get = () => this.db.list('/movies');
}
There is my component:
import { Component, OnInit } from '@angular/core';
import { MoviesService } from './movies.service';
@Component({
selector: 'app-movies',
templateUrl: './movies.component.html',
styleUrls: ['./movies.component.css']
})
export class MoviesComponent implements OnInit {
movies: any[];
constructor(private moviesDb: MoviesService) { }
ngOnInit() {
this.moviesDb.get().subscribe((snaps) => {
snaps.forEach((snap) => {
this.movies = snap;
console.log(this.movies);
});
});
}
}
And there is mmy pug:
ul
li(*ngFor='let movie of (movies | async)')
| {{ movie.title | async }}
In your MoviesService you should import FirebaseListObservable in order to define return type
FirebaseListObservable<any[]>
then get() method should like this-
this get() method will return FirebaseListObervable of movies list
In your MoviesComponent should look like this
Then you can easily iterate through movies without async pipe as mivies[] data is not observable type, your html should like this and your html should be this
if you declear movies as a
then you should simply call
and your html should be this
You get this message when you've used async in your template, but are referring to an object that isn't an Observable.
So for examples sake, lets' say I had these properties in my class:
Then in my template, I refer to it this way:
instead of:
You wouldn't need the job:Job property if you use the async pipe, but it serves to illustrate a cause of the error.
async
is used for binding to Observables and Promises, but it seems like you're binding to a regular object. You can just remove bothasync
keywords and it should probably work.I found another solution to get the data. according to the documentation Please check documentation link
In service file add following.
In Component add following.
In your html file add following.
You should add the pipe to the
interpolation
and not to thengFor
Reference docs