@ angular / fire实时数据库:如何获取其下的键和值

时间:2018-10-14 04:40:35

标签: angular firebase ionic-framework angularfire5

背景

我正在使用离子4和“ @ angular / fire”:“ ^ 5.0.2”

问题

我想列出配置文件节点下的所有值,这是我的firebase结构: Brian Firebase

我想获取密钥,位置,ownerName和restoName。稍后,我将使用该密钥打开另一个节点。对于下面的代码,我想显示位置,ownerName和restoName。

我的代码

home.ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController } from 'ionic-angular';

import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFireDatabase, AngularFireList } from '@angular/fire/database';
import { UserProfile } from './../../models/user-profile';
import { RestoProfile } from './../../models/resto-profile';


@IonicPage()
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  restoProfileData: AngularFireList<RestoProfile[]>;
  restoProfileRef: AngularFireList<RestoProfile[]>;

  constructor(private afAuth: AngularFireAuth, private afDatabase: AngularFireDatabase,
    private toast: ToastController,
    public navCtrl: NavController, public navParams: NavParams) {
  }

  ionViewDidLoad() {
    this.afAuth.authState.subscribe(data => {
      if(data && data.email && data.uid){
        this.toast.create({
          message: `Welcome to brianApp-customer, ${data.email}`,
          duration: 3000
        }).present();

        this.restoProfileRef = this.afDatabase.list<RestoProfile>(`profile/`);
        this.restoProfileData = this.restoProfileRef.snapshotChanges();

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

home.html

<ion-list>
  <ion-item *ngFor="let data of restoProfileData | async">
    <h2>Key: {{ data.payload.key }}</h2>
    <h2>Location: {{data.payload.val().location}}</h2>
    <h2>ownerName: {{data.payload.val().ownerName}}</h2>
    <h2>restoName: {{data.payload.val().restoName}}</h2>
  </ion-item>
</ion-list>

此代码结果为Object(...) is not a function

任何帮助将不胜感激。谢谢

2 个答案:

答案 0 :(得分:0)

您可以直接从data对象访问配置文件属性。

<ion-list>
  <ion-item *ngFor="let data of restoProfileData | async">
    <h2>Key: {{ data.key }}</h2>
    <h2>Location: {{data.location}}</h2>
    <h2>ownerName: {{data.ownerName}}</h2>
    <h2>restoName: {{data.restoName}}</h2>
  </ion-item>
</ion-list>

答案 1 :(得分:0)

解决了!

我需要将rxjs和rxjs-compat更新到版本6.3.3。

npm install rxjs@6.3.3 rxjs-compat@6.3.3 --save

代码没有变化。仍然与我上面的问题相同。