Existing code(Working fine) :
.then((result) => {
this.triggerAction(result.id);
}).catch((error) => {
this.errorMsg(error);
});
when i try to add condition inside the .then throws error.
.then((result && result.id) => {
this.triggerAction(result.id);
}).catch((error) => {
this.errorMsg(error);
});
I need to check both result and result.id is coming in the response and throws error id if not available in the result .
>Solution :
To throw an error if result.id is missing, you should do the following:
.then((result) => {
if(!result.id) {
throw new Error("result.id is missing!");
}
this.triggerAction(result.id);
}).catch((error) => {
this.errorMsg(error);
});
You can also do throw result if you just want to pass result to the .catch() block