【问题标题】:Ionic 3 Angular Fire payload.val() return nullIonic 3 Angular Fire payload.val() 返回 null
【发布时间】:2018-07-31 22:05:47
【问题描述】:

我正在使用 Firebase 数据库来存储个人资料信息。如何检查当前配置文件是否已填写或为空?目前我正在使用profile.payload.val(),如果填写了profile,它应该返回true,但在两种情况下都返回null。

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

【问题讨论】:

    标签: javascript angular firebase ionic-framework firebase-realtime-database


    【解决方案1】:

    如果你想访问你需要使用的数据

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 2019-02-18
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多