开发者问题收集

firebase 的数据无法加载?

2018-04-12
438

我试图从 firebase 数据库中获取某个 userId 的数据。但什么也没发生?我正在创建一个 Profile 类型的 AngularFireObject

像这样:

  profileData: AngularFireObject<Profile>;

代码中没有错误。HTML 按照我喜欢的方式显示,但没有数据。 唯一不正确的是,方法 ionViewDidLoad 显示为未使用。但是当我在构造函数中传递代码时,它没有任何效果。

有人能帮我吗?非常感谢!

import { Component } from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {AngularFireAuth} from 'angularfire2/auth';
import {AngularFireDatabase, AngularFireObject} from 'angularfire2/database';
import {Profile} from '../../shared/models/profile';


/**
 * Generated class for the HomePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
})
export class HomePage {

  profileData: AngularFireObject<Profile>;

  constructor(private afAuth: AngularFireAuth, private afDatabase: AngularFireDatabase, private toast: ToastController,
              public navCtrl: NavController, public navParams: NavParams) { }
ionViewDidLoad() {
 this.afAuth.authState.subscribe(data => {
      console.log(data.uid);
      if (data && data.email && data.uid) {
        console.log(data.email);
        console.log(data.uid);

        this.toast.create({
          message: `Welcome to The APP, ${data.email}`,
          duration: 5000
        }).present();

        this.profileData = this.afDatabase.object(`/profile/${data.uid}`)

        console.log(this.profileData);

      } else {
        this.toast.create({
          message: `Could not find authentication details`,
          duration: 3000
        }).present();
      }
    })
  }
}

这是我想要加载数据的 HTML。

profile.ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {AngularFireAuth} from 'angularfire2/auth';
import {Profile} from '../../shared/models/profile';
import {AngularFireDatabase} from 'angularfire2/database';


@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'))

    })
  }

}




 <p>Username: {{(profile | async)?.username }}</p>
  <p>First Name: {{(profile | async) ?.lastName }}</p>
  <p>Last Name: {{(profile | async) ?.firstName}}</p>

Console.log

2个回答

路径中存在问题。您的路径中有一个用户 ID,它与个人资料连接在一起,但没有使用 斜杠 (/) 将其分隔开

尝试重新更正路径,

this.afDatabase.object(`/profile/${data.uid}`)

要检索数据和视图,请执行以下操作

 this.afAuth.authState.subscribe(data => {
      if (data && data.email && data.uid) {
        this.afDatabase.database.ref().child(`/profile/${data.uid}`).once('value', (snapshot) => {
          let result = snapshot.val();
          for (let k in result) {
            this.profileData = result[k];
          }
        });
      }
 });

**这是 html **

  <p>Username: {{ profileData.username }}</p>
  <p>First Name: {{ profileData.lastName }}</p>
  <p>Last Name: {{( profileData.firstName}}</p> 
Sarasa Gunawardhana
2018-04-12

在 Homepage.ts 中 您只需在第 27 行添加 take(1),如下所示: this.afAuth.authState.take(1).subscribe(data => {{

Mahdi
2018-12-01