I dont know why the code is not running. Even the console.log('1') never appears.
ngOnInit() {
this.getContacts();
this.getClients();
}
getContacts(): Observable<any[]> {
return this.http.get('/api/contacts')
.map((res: any) => res.json()).flatMap((contacts: any[]) => {
if (contacts.length > 0) {
console.log(1);
const observables = Observable.forkJoin(
contacts.map((contact: any) => {
return this.http.get('/api/client/' + contact.company_name)
.map((res: any) => {
let company_name: any = res.json();
contact.company_name = name;
console.log(contact.company_name);
return contact;
});
})
);
console.log(observables);
return observables;
}
return Observable.of([]);
}
);
}
The plain response of localhost:4200/api/contacts is:
[{"_id":"59f43f363a2fc421c00ad8b2","anrede":"sir","titel":"dr.","vorname":"name1","nachname":"surname1","company":"59f43f0e3a2fc421c00ad8b0","__v":0},{"_id":"59f43f443a2fc421c00ad8b3","anrede":"mrs","titel":"prof.","vorname":"name2","nachname":"surname2","company":"59f43f1a3a2fc421c00ad8b1","__v":0}]
The plain response of localhost:4200/api/client is:
{"_id":"59f43f1a3a2fc421c00ad8b1","name":"company2","street":"street 2","zipcode":"45678","city":"city2","__v":0}
I followed this