我正在使用 Firebase 数据库来存储个人资料信息。如何检查当前个人资料是否已填写或为空?目前我正在使用 profile.payload.val(),如果填写了配置文件,它应该返回 true,但在这两种情况下都返回 null。
数据.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();
}
}
登录.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();
}
}
}
最佳答案
如果你想访问数据,你需要使用
this.data.getProfile(<User>event.result).subscribe(profile => {
console.log(profile.payload.payload.data());
})
引用号:https://github.com/angular/angularfire2/blob/master/docs/firestore/collections.md
关于javascript - Ionic 3 Angular Fire Payload.val() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51613329/