I have a REST endpoint that I am calling with Angular Http. I need to chain two calls, then use the results from each to create an object to return.
Pseudo code:
- POST new entity to server and get a UID back
- PATCH entity content to server, using UID returned
- create an object that combines entity content and UID
- return object to caller (subscribe)
I thought this would do it, but I get an error;
addClient(clientData) {
let clientUid;
return this.http.post(`/clients`, clientData)
.flatMap((newClient:any) => {
clientUid = newClient.name;
return this.http.patch(`/spec/${clientUid}`, clientData)
.flatMap((updatedClient:any)=> {
return {
uid: clientUid,
...updatedClient,
}
})
});
}
//consume
this.clientService.addClient(clientData)
.subscribe((response:any) =>{ }
TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
How can I:
- fix the error and have addClient return my desired object?
- Also, is there a way I can consume the UID in the final flatMap without having to set the local variable (clientUid )?