0

I have the following array in component class (Angular 5):

this.collectionAdmins = ['uid_73hd', 'uid_38ng'];

Since I have tables in my database(firebase) with users info, I want to loop through the this.collectionAdmins array, retrieve each user information from the database, So will be able to display that info in the template.

'uid_73hd': {
  "bio": "about me info",
  "email": "[email protected]",
  "status": "online",
  "uid": "uid_73hd",
  "username": "mary"
},
'uid_38ng': {
  "bio": "about me info",
  "email": "[email protected]",
  "status": "online",
  "uid": "uid_38ng",
  "username": "john"
}

How can I achieve this?

4

1 Answer 1

1

Because you’re using Firestore and AngularFire2, we can using following solution:

import { combineLatest } from 'rxjs/observable/combineLatest';

export class YourComponent {
  collectionAdmins = ['uid_73hd', 'uid_38ng'];


  constructor(private afs: AngularFirestore) {

    const abs = this.collectionAdmins
      .map(id => afs.doc<User>(`users/${id}`).valueChanges()); // users table, replace with your real table
    this.collections$ = combineLatest(abs);
  }
}

p/s: some other operators to combine observable: forkJoin, zip. Or Promise.all if you want to using Promise instead of Observable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.