Currently, I am diving into the world of Angular 6 with RxJS 6 and have stumbled upon a question regarding the scenario where I need to return a null or empty Observable in case the response fails or encounters an exception. In this situation, my assumption is that the remote API will provide an object of type IOptionResponse, which consists of a message string indicating either 'SUCCESS' or 'FAILED', along with a model that is an array of 'IOption' objects.
export interface IOptionResponse {
message: string;
model: IOption[];
}
One of my service methods aims to return an Observable containing an array of IOption elements, which represents the "model" from the result of the remote API.
loadIOptionMembersRelationship(): Observable<IOption[]> {
return this.httpClient.get<IOptionResponse>('${environment.apiUrl}/api/member/XXX')
.map(
(response) => {
console.log(response);
// If the response message indicates success, return the IOption[] model
if (response.message == responseMessage.Success) {
return response.model;
}
else {
// Here lies the challenge of returning a null or empty Observable
// What would be the correct approach to handle this scenario?
}
}
);
}
I came across a similar post on StackOverflow, but none of the suggested solutions seem to work. It's unclear whether it's due to outdated information, changes in RxJS 6, or potential TypeScript issues...