My goal is to execute multiple requests in parallel, fetch the results, and combine them. To achieve this, I have implemented the following function:
getStudent(query): Observable<any> {
const code = this.http.get(
`http://localhost:8090/etudiantFiltre?codEtudiant=${query}`
);
const prenom = this.http.get(
`http://localhost:8090/etudiantFiltre?prenom1=${query}`
);
const nom = this.http.get(
`http://localhost:8090/etudiantFiltre?patronyme=${query}`
);
return Observable.forkJoin([code, nom, prenom]).map(responses => {
console.log(`code : ${code}
nom : ${nom}
prenom : ${prenom}`);
return [].concat(...responses);
});
}
While this function works fine for single-word queries like 'John', I needed to enhance it to support queries with spaces. To address this, I modified the function to split the query by spaces and then call the original method on each element. I then used the merge method to combine the results.
getAllStudent(query) {
const studentObservable: Observable<Response>[] = [];
query.split(' ').forEach(element => {
studentObservable.push(this.getStudent(element));
});
return Observable.merge(studentObservable);
}
For making the actual method call, I have the following in my ngOnInit:
ngOnInit() {
this.getAllStudent('name firstname').subscribe(data => {
this.st = data;
});
}
However, when I try to print the result of the method, I'm getting unexpected output and I'm unsure why:
{
"_isScalar": false,
"source": {
"_isScalar": false,
"sources": [
{
"_isScalar": false,
"source": {
"_isScalar": false,
"source": {
"_isScalar": false,
"source": {
"_isScalar": true,
"value": {
"url": "http://localhost:8090/etudiantFiltre?codEtudiant=firstname",
"body": null,
"reportProgress": false,
"withCredentials": false,
"responseType": "json",
"method": "GET",
"headers": {
"normalizedNames": {},
"lazyUpdate": null,
"headers": {}
...