0

I am trying to get a user details from a angularFire2 database. Therefore instead of using the FirebaseListObservable, I am using FirebaseObjectObservable since I am expecting just a user from the path.

This is the path and the colums through which I am deriving the user data

  path: /projects/users/
  columns: userID,username

  data
     userID:"001",
     username:"Icomin"

And this the home.ts

   import { Component } from '@angular/core';
   import { NavController,AlertController,ActionSheetController } from 'ionic-angular';
   import{AngularFireDatabase,FirebaseListObservable,FirebaseObjectObservable} from 'angularfire2/database';
  import {Observable} from 'rxjs/Observable';



@Component({
   selector: 'my-child-component',
   template: `<h1>Welcome {{ (projects | async)}}</h1>` // The template
})
export class HomePage{


projects: FirebaseObjectObservable <any>;

 constructor(af:AngularFireDatabase) {

      this.projects = af.object(`/projects/users`);
  }

I implement the template below, however the returnsWelcome [object object]

The template implementation

 template: `<h1>Welcome {{ (projects | async)}}</h1>`

How do I get the user details from the above path and set it into the template

3
  • See stackoverflow.com/questions/41755579/… Commented May 30, 2017 at 1:35
  • @FrankvanPuffelen I am not joiing anything, all I am trying to do is get details using the object observables Commented May 30, 2017 at 1:38
  • Ah, sorry about that. Your code is reading /projects/users, which is a list of data. So your HTML will need to loop over that. Alternatively, make your code read a single user /projects/users/user1. Commented May 30, 2017 at 1:45

1 Answer 1

1

As you mention , you get Welcome [object object] from projects variable. It's mean that you receive a list from this call af.object('/projects/users') .
I dont really know what exactly you want to do with that list but you few options:
1 - show all users in list :

template: ' <h1 *ngFor="let proj of projects | async">{{ proj.username }}</h1>

this will display all user's names , one by one.
2 - show specific user:
(you need to choose the filter criteria: userID or username)

export class HomePage{
  project: FirebaseObjectObservable <any>;
  constructor(af:AngularFireDatabase) {
    this.project = af.list(`/projects/users`,{
      query: {
          orderByChild: 'userID',      //or orderByChild: 'username',
          equalTo: <someUserID>        // equalTo: <someUsername>
      }
    });
  }
}

and then the template to display the current username:
template: <h1>{{ project.username }}</h1>

Hope it fits what you wanted.

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

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.