Brian Ivander T. P.
Brian Ivander T. P.

Reputation: 485

@angular/fire Real Time Database: How to get key and value under it

Background

I am using ionic 4 with "@angular/fire": "^5.0.2"

The Issue

I want to list all the values under profile node, here's my firebase structure: Brian Firebase

I want to get the key, location, ownerName, and restoName. I will use the key later on to open another node. And for the code below, I want to display the location, ownerName, and restoName.

My Code

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

In 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>

This code results to Object(...) is not a function.

Any help would be appreciated. Thank you

Upvotes: 0

Views: 1788

Answers (2)

Brian Ivander T. P.
Brian Ivander T. P.

Reputation: 485

Solved it!

I need to update my rxjs and rxjs-compat to version 6.3.3.

npm install [email protected] [email protected] --save

There's no change in code. Still the same with my question above.

Upvotes: 0

Sunil
Sunil

Reputation: 11243

You can access profile property directly from the data object.

<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>

Upvotes: 1

Related Questions