Volodymyr Humeniuk
Volodymyr Humeniuk

Reputation: 3801

Return object value from Firebase realtime DataBase

i have a list of objects (users) in Firebase. Every object looks like this: enter image description here

And i want get 'banned' value for user.

Now my func looks like this:

  isBanned() {
    return this.db.object(`users/${this.authState.uid}/banned`)
  }

and it's return nothing. How should i write this correctly?

I'm using angular 5 and AngularFire2.

Thanks for help.

Upvotes: 0

Views: 789

Answers (2)

James Daniels
James Daniels

Reputation: 6981

The core of the problem is that authState is async. I'd recommend you rewrite this as an Observable mapped from AngularFireAuth's authState:

this.isBanned$ = this.afAuth.authState.switchMap(u => u ? this.db.object(`users/${u.id}/banned`).valueChanges() : Observable.of(undefined))

Upvotes: 1

Ronnie Smith
Ronnie Smith

Reputation: 18585

It's possible your .onAuthStateChangedasynch method has not resolved when this function gets fired. Try and see by console'ing out the ${this.authState.uid} from within that function. If it's null that's your issue (asnych issue)

Assuming you do have the uid, try and just get the whole parent node then snap.val().banned.

Upvotes: 1

Related Questions