5

I am trying to fetch a list of users from my firestore database, and determine if the logged in user is an admin (i.e. he's in the list).

This is my code:

isAdmin(user: any): boolean {
    var col = this.afs.collection('admins');
    var docRef = col.doc('mini-admins');
    docRef.ref.get().then(function (doc) {
      if (doc.data().user == user)
        return true;
    }).catch(function (error) {
      console.log("Error getting document:", error);
    });
    return false;
}

However, even though the user is an admin, the function doesn't work. It looks like it isn't waiting for it to finish before proceeding.

How do I fix this?

amitairos
  • 2,907
  • 11
  • 50
  • 84

2 Answers2

11

It's not working because it's asynchronous. To make it work you need to change it slightly to:

isAdmin(user: any): Observable<boolean> {
    return new Observable(observer => { 
       var col = this.afs.collection('admins');
       var docRef = col.doc('mini-admins');
       docRef.ref.get().then(function (doc) {
          if (doc.data().user == user) {
              observer.next(true);
              observer.complete();
          }   
       }).catch(function (error) {
          observer.error(error);
          observer.complete();
          console.log("Error getting document:", error);
       });
    });
}

Then in your code when you want to check if the user is admin, call this function in the following way:

this.isAdmin(user).subscribe(success => { ... }, error => { ... });
Vitalii Chmovzh
  • 2,825
  • 4
  • 14
  • 28
5

.then indicates ref.get is an async function. You can't wait for async functions. You can only return the Promise returned from it or a new one that is properly chained, and pass another callback on the callers site to execute code when the promise completes:

isAdmin(user: any): Promise<boolean> {
    var col = this.afs.collection('admins');
    var docRef = col.doc('mini-admins');
    // vvv added return
    return docRef.ref.get().then(function (doc) {
      return doc.data().user == user;
    }).catch(function (error) {
      console.log("Error getting document:", error);
    });
}

and then use it like

this.isAdmin(user).then(response => {
  if(response) {
    ...
  } else {
    ...
  }
});
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567