I am facing an issue with a service that uses a Behavior subject which is not triggering the next() function.
Upon checking, I can see that the method is being called as the response is logged in the console.
errorSubject = new BehaviorSubject<any>(
{
exception: false,
message: '',
segNum: ''
}
);
error = this.errorSubject.asObservable();
responseHandler(response) {
let obj = {};
if (response.success) {
return response.payload;
} else {
obj = {
exception: true,
message: response.exception.message,
segNum: response.exception.seqNum
};
}
console.log(obj);
this.errorSubject.next(obj);
}
When subscribing to the error like below:
error;
ngOnInit() {
this.error = this.apiHandlerService.error.subscribe(obj => {
console.log(obj);
if (obj.exception) {
this.openModal();
}
});
}
Every API call goes through this method. If an exception is found, it sends the error details to the modal component, otherwise, it sends the payload.
However, when the else condition is triggered, it appears to be sending the initial value of the errorSubject
rather than using the next()
method.
Any suggestions on how to fix this?
Following the suggestion provided below, I attempted rearranging the method so that it returns at the end but the issue persists:
responseHandler(response) {
let obj;
if (!response.success) {
obj = {
exception: true,
message: response.exception.message,
segNum: response.exception.seqNum
};
console.log(response);
} else {
obj = response.payload;
}
this.errorSubject.next(obj);
return obj;
}