Home > Software engineering >  Angular firestore perform another query from previous query
Angular firestore perform another query from previous query

Time:03-17

I using @angular/fire to query data and I want to saving the firebase cost by reducing the number call of firebase query.

I have 2 function that query from the same collection. First is get all data and second is query with condition. How the second function to query data from the first function so it only call 1 time if firebase query

constructor(private _afs: AngularFirestore)

function 1 get all data from user

this._afs.collection('firebase-user-table').valueChanges().subscribe(data => {
        console.log('User data:', data);
      })

function 2 get only which user is_admin is true

this._afs.collection('firebase-user-table', (ref) => ref.where(is_admin, '==', true)).valueChanges().subscribe(data => {
        console.log('Admin data:', data);
      })

CodePudding user response:

You could do something like this using rxjs:

let allUsers$ = this._afs.collection('firebase-user-table').valueChanges();

let adminUsers$ = allUsers$.pipe(
map((users) => users.filter(user)) => user.is_admin)
)

Then subscribe to each observable as needed

  • Related