Rika
Rika

Reputation: 353

How to retrieve data from Firebase database after passing currentUserID

This is my firebase database and table:

firebase database and table.

I want to retrieve data from table 'active' by using userID. I tried it like this in my service.ts file.

getCurrentUserData(userID){
    return new Observable(obs => {
      this.db.list('/active')
      .valueChanges()
      .subscribe(res => {
         console.log(res)
     })
   });
}

assume current userID userID = "oiV0Q09hLbWv0nhFUeFd94aWF3f1" and that table has so many another UserID also.I want to get current user details only.Using that code i receive all data in table.How can get only current user data using UserID.How can i edit my code ?

Upvotes: 1

Views: 114

Answers (2)

Renaud Tarnec
Renaud Tarnec

Reputation: 83191

Based on the angulafire2 doc, I think you should do like the following. In other words, you should narrow your query in such a way the filtering is done at the level of the database itself and not on the client side.

 getCurrentUserData(userID){
    return new Observable(obs => {
      this.db.list('/active', ref => ref.orderByKey().equalTo('oiV0Q09hLbWv0nhFUeFd94aWF3f1'))
      .valueChanges()
      .subscribe(res => {
         console.log(res)
     })
   });
}

Note that the angulafire2 doc gives also some explanations about Dynamic Querying.

You can also read the Firebase doc about querying, here, which gives more details on querying techniques.

Upvotes: 1

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41455

use filter operator to filter out the result by id

 getCurrentUserData(userID){
    return new Observable(obs => {
      this.db.list('/active')
      .filter(p => p.userID === "oiV0Q09hLbWv0nhFUeFd94aWF3f1")
      .valueChanges()
      .subscribe(res => {
         console.log(res)
     })
   });
}

Upvotes: 0

Related Questions