Reputation: 23
I am fetching data from firestore, and now i want to push data into an empty array in same order of document id provided. When document id is present data should be fetched and pushed into array if no document id is present "No Salesman Assigned" should be pushed into the array.
getSalesmanName(signupId){
let name;
if(signupId){
this.db.collection('users/').doc(signupId)
.snapshotChanges().pipe().map( res=> {
const data=res.payload.data() as Users
return data;
})
.subscribe(res=>{
this.salesmanArray.push(res.first_name);
})
}else{
this.salesmanArray.push("No Salesman Assigned")
}
}
Expected Result: Name1, Name2, No Salesman Assigned, Name3, No Salesman Assigned
Actual Result: No Salesman Assigned, No Salesman Assigned, Name1, Name2, Name3
Upvotes: 0
Views: 119
Reputation: 39472
Give this a try:
getSalesmanName(signupIds) {
const observableArray = signupIds.map(signupId => {
if (signupId) {
return this.db.collection('users/').doc(signupId)
.valueChanges()
.pipe(
map(res => (res as User).first_name)
)
} else {
return of('No Salesman Assigned');
}
});
combineLatest(observableArray)
.subscribe(res => this.salesmanArray = res);
}
Here's a Working Sample StackBlitz for your ref.
PS: I'm assuming that you're using Angular 7.
Upvotes: 0