问题
I want to perform some firebase operation inside a loop in angular. When I send an HTTP request to firebase it returns an array of observables so using forkjoin i convert this array of observables into single observables. Now the problem is when I subscribe to these new observables I didn't get anything
get_student_email() {
this.get_project_service
.get_project_by_doc_id(this.batchID, this.projectID)
.subscribe((res) => {
this.response = res;
this.email = this.response.student;
let data = this.email.map((email) =>
this.get_student_service.get_student_by_email_id(email)
);
let info = forkJoin([data]);
console.log('info:', info);
info.subscribe((res) => {
console.log(res);
console.log('Execute');
});
});
}
Output:info: Observable {_isScalar: false, _subscribe: ƒ}
I also try this. In this time it again returns observables
let data = this.email.map((email) =>
this.get_student_service.get_student_by_email_id(email)
);
let info = forkJoin([data]);
console.log('info:', info);
info.subscribe((res) => {
console.log(res);
console.log('Execute');
});
});
Output:-
info: Observable {_isScalar: false, _subscribe: ƒ}
[Observable]
Execute
Here is my service
get_student_by_email_id(email) {
return this.fire_store
.collection('User', (ref) => ref.where('email', '==', email))
.valueChanges();
}
I don't know what is the main reason behind this problem. What I want is that after loop complete its execution it return single array object contain information about all the user which belongs to that email array(this.email)
回答1:
The problem is with the code forkJoin([data])
.
Your data
it's already an array of Observables, so you just pass it to forkJoin
like this: forkJoin(data)
.
来源:https://stackoverflow.com/questions/64908123/unable-to-subscribe-after-forkjoin