I have a method that returns an Observable as follows:
public myMethod(params): Observable<CertainType> {
// some code goes here;
return defer(() => this.customHttp.request<XML>(config, ctx)).pipe(
map(
(xmlRes: string) => {
const xmlHandler = new strongsoap.soap.XMLHandler();
const rawSFResponse = xmlHandler.xmlToJson(null, xml, null);
const res = rawSFResponse["Body"]["SFResponse"]["SFResult"];
// more code goes here;
}
)
)
}
The problem with this piece of code is that I receive an error in the console that says
Cannot read property 'SFResult' of undefined
.
And if I try console-logging the rawSFResponse
object it gets printed out but when I click on it it says "value below was evaluated just now".
As far as I understand it happens because this statement res = rawSFResponse["Body"]["SFResponse"]["SFResult"];
is executed before the res object is fully formed, namely at the moment it gets executed the content of the SFResult
field is still undefined.
Is it correct? If so, then what would be the best way to wait for it to be formed. Maybe I have to wrap the parsing function into a promise and then use await
on it?