1

I'm simply trying to get a username value from my firebase database and display it in a console log statement but I am having trouble doing this. This is what I have so far.

Right now all it is doing is getting the object and not the actual child value. How do I get the child value so then it can be displayed.

enter image description here

getUsername.ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams} from 'ionic-angular';
import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireDatabase, FirebaseObjectObservable, FirebaseListObservable  } from 'angularfire2/database';

@IonicPage()
@Component({
  selector: 'page-profile-setup',
  templateUrl: 'profile-setup.html',
})
export class ProfileSetupPage {
  profileData: FirebaseObjectObservable<Profile>

  constructor(private afAuth: AngularFireAuth, private afDatabase: AngularFireDatabase) {

  showUsername($event) {
    let data = this.profileData = this.afDatabase.object(`profile/`)
    console.log(data.username);
  }
 }
}
4
  • You will need to somehow use the ID of the object to locate the username. This is unavoidable. You can either add it to the query, or use it when reaching into the object. Commented Jan 14, 2018 at 21:17
  • okay so if I add profile/WhateverIDIs how would I get the actual username value? Commented Jan 14, 2018 at 21:24
  • Perform the query and log its results. It should be evident how to find the fields you're looking for. I don't work with Angular at all so I don't know the specifics. Commented Jan 14, 2018 at 21:34
  • Okay thanks for the help! Commented Jan 14, 2018 at 21:38

2 Answers 2

1

You have to subscribe to the data first and put it in a object to use the data.

Sign up to request clarification or add additional context in comments.

Comments

0

Define model(interface) named user(username, age) and import in getUsername.ts. And also you need to pass the unique userid as the argument.

showUsername($event) {
    let data = this.afDatabase.object(`profile/`+<string>userid);
    data.valuechanges().subscribe( user => console.log(user.username));
  } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.