Firebase request strange behaviour

24 Views Asked by At

I want to fetch all users from the same company as the logged in user.

With getUsersFromCompany(this.userData.company) I do only get the user itself and nobody else. When changing manually to another Company-ID (which is not the same ID as this.userData.company) all users are loading: getUsersFromCompany('anotherCompanyID)

 getUsersFromCompany(company) {
    return this.afs.collection('users', ref => ref.where('company', '==', company)) 
      .valueChanges({ idField: 'uid' }).pipe(take(1)) as Observable<UserData>
  }

It seems to block all users when the company ID is the same, but why/how?

My firebase security rules for testing are set to

 match /users/{userId} {
     allow read: if request.auth != null    
}

Persistance was on, is now off

1

There are 1 best solutions below

0
bastifix On

After a lot of testing around it stayed really confussing. But it seemed to have something to do with angularfire...

Solution for me: Switching from

getUsersFromCompany(company) {
    return this.afs.collection('users', ref => ref.where('company', '==', company)) 
      .valueChanges({ idField: 'uid' }).pipe(take(1)) as Observable<UserData>
  }

to

 async getUsersfromCompany(company) {
    const db = getFirestore();
    var users: any = []
    try {

      const q = query(collection(db, "users"), where("company", "==", company));

      const querySnapshot = await getDocs(q);

      querySnapshot.forEach(doc => {
        users.push(doc.data())
      })
    } catch (error) {
      console.log(error);
    }
    return users
  }