I’m encountering an issue while attempting to replicate the steps from a specific YouTube tutorial. At the 8:22 mark of this video, I’m facing the following error:
Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'
https://i.sstatic.net/Tp8Zc.png
This is how the data is structured within firebase. https://i.sstatic.net/AsnJN.png
The information gets stored in firebase through a profile.html page.
<ion-content padding>
<ion-item>
<ion-label floating>Username</ion-label>
<ion-input [(ngModel)]="profile.username"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>First Name</ion-label>
<ion-input [(ngModel)]="profile.firstname"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Last Name</ion-label>
<ion-input [(ngModel)]="profile.lastname"></ion-input>
</ion-item>
<button ion-button block (click)="createProfile()">Create Profile</button>
</ion-content>
This is what the profile.ts file consists of:
import { Profile } from '../../models/profile';
import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireDatabase } from 'angularfire2/database';
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
/**
* Generated class for the ProfilePage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-profile',
templateUrl: 'profile.html',
})
export class ProfilePage {
profile = {} as Profile;
constructor(
private afAuth: AngularFireAuth,
private afDatabase: AngularFireDatabase,
public navCtrl: NavController,
public navParams: NavParams) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad ProfilePage');
}
createProfile(){
this.afAuth.authState.take(1).subscribe(auth => {
this.afDatabase.object(`profile/${auth.uid}`).set(this.profile)
.then(() => this.navCtrl.setRoot('HomePage'));
})
}
}
The structure of the models/profile.ts is as follows:
export interface Profile {
username: string;
firstName: string;
lastName: string;
}
At this particular juncture in the tutorial, our aim is to simply display the username on the homepage.
This is my home.ts file setup:
import { Item } from './../../models/item/item.model';
import { Profile } from './../../models/profile';
import { AngularFireDatabase, FirebaseObjectObservable } from 'angularfire2/database';
import { Component } from '@angular/core';
import { NavController, IonicPage, ToastController } from 'ionic-angular';
import { ShoppingListService } from '../../services/shopping-list/shopping-list.service';
import { Observable } from 'rxjs/Observable';
import { AngularFireAuth } from 'angularfire2/auth';
@IonicPage()
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
profileData: FirebaseObjectObservable<Profile>
shoppingList$: Observable<Item[]>
constructor(
private afAuth: AngularFireAuth,
private afDatabase: AngularFireDatabase,
private toast: ToastController,
public navCtrl: NavController,
private shopping:ShoppingListService) {
this.shoppingList$ = this.shopping
.getShoppingList()
.snapshotChanges()
.map(
changes => {
return changes.map(c => ({
key: c.payload.key, ...c.payload.val()
}));
});
}
ionViewWillLoad(){
this.afAuth.authState.subscribe(data => {
if(data && data.email && data.uid){
this.toast.create({
message: `Welcome to the Jungle, ${data.email}`,
duration: 3000
}).present();
this.profileData = this.afDatabase.object(`profile/${data.uid}`)
} else {
this.toast.create({
message: `Could not log you in`,
duration: 3000
}).present();
}
}
)}
The layout of my home.html page looks like this:
<ion-header>
<ion-navbar color="primary">
<ion-title>
Shopping List
</ion-title>
<ion-buttons end>
<button navPush='AddShoppingItemPage' ion-button>
<ion-icon name="add"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content padding>
<p>Username: {{(profileData | async)?.username}}</p>
<ion-list>
<ion-list-header>
Items
</ion-list-header>
<ion-item *ngFor="let item of shoppingList$ | async" detail-push navPush="EditShoppingItemPage" [navParams]="{item: item}">
{{item.name}}
</ion-item>
</ion-list>
</ion-content>
It appears that either this line in home.ts:
this.profileData = this.afDatabase.object(`profile/${data.uid}`)
or this line:
profileData: AngularFireObject<Profile>
or this line:
import { AngularFireDatabase, FirebaseObjectObservable } from 'angularfire2/database';
is causing the problem.
However, the resolution remains unclear. Although I have diligently followed the provided tutorial, it seems to be functioning seamlessly for the creator. Despite being only 5 months old, technology evolves swiftly, rendering tutorials outdated promptly. As a novice, rectifying errors that are incomprehensible becomes arduous, hindering the learning process significantly.