Reputation: 164
I'm using Firebase Database to store a profile information. How do I check if current profile is filled in or it is empty? Currently I'm using profile.payload.val(), which should return true if profile is filled in, but returns null in both cases.
data.service.ts
import { Injectable } from '@angular/core';
import { AngularFireDatabase, AngularFireObject } from 'angularfire2/database';
import { User } from 'firebase/app';
import { Profile } from '../../models/profile/profile.interface';
@Injectable()
export class DataService {
profileObject: AngularFireObject<Profile>
constructor(private database: AngularFireDatabase) {
}
getProfile(user: User) {
this.profileObject = this.database.object(`/profiles/${user.uid}`);
return this.profileObject.snapshotChanges();
}
}
login.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController } from 'ionic-angular';
import { LoginResponse } from '../../models/login/login-response.interface'
import { DataService } from '../../providers/data/data.service';
import { User } from 'firebase/app';
import { Subscription } from 'rxjs/Subscription';
@IonicPage()
@Component({
selector: 'page-login',
templateUrl: 'login.html',
})
export class LoginPage {
constructor(
private data: DataService,
private toast: ToastController,
private navCtrl: NavController,
private navParams: NavParams) {
}
login(event: LoginResponse) {
if(!event.error) {
this.toast.create({
message: `Welcome to Beep, ${event.result.email}`,
duration: 3000
}).present();
this.data.getProfile(<User>event.result).subscribe(profile => {
console.log(profile.payload.val());
profile.payload.val() ? this.navCtrl.setRoot('TabsPage') : this.navCtrl.setRoot('EditProfilePage');
})
}
else {
this.toast.create({
message: event.error.message,
duration: 3000
}).present();
}
}
}
Upvotes: 0
Views: 355
Reputation: 27303
If you want to access data you need to use
this.data.getProfile(<User>event.result).subscribe(profile => {
console.log(profile.payload.payload.data());
})
Ref:https://github.com/angular/angularfire2/blob/master/docs/firestore/collections.md
Upvotes: 1