Código existente (funciona bien):
.then((result) => { this.triggerAction(result.id); }).catch((error) => { this.errorMsg(error); });
cuando trato de agregar una condición dentro del .entonces arroja un error.
.then((result && result.id) => { this.triggerAction(result.id); }).catch((error) => { this.errorMsg(error); });
Necesito verificar tanto el resultado como el resultado. ID aparece en la respuesta y arroja una identificación de error si no está disponible en el resultado.
Para arrojar un error si falta result.id
, debe hacer lo siguiente:
.then((result) => { if(!result.id) { throw new Error("result.id is missing!"); } this.triggerAction(result.id); }).catch((error) => { this.errorMsg(error); });
También puede throw result
si solo desea pasar el result
al bloque .catch()
.then((result) => { result?.id && this.triggerAction(result.id); }) .catch((error) => { this.errorMsg(error); });
result inside .then es la respuesta que recibe en caso de éxito. No puede aplicar condiciones sobre la marcha dentro de los parámetros de la función. Tienes que comprobar dentro de la función de devolución de llamada. Si los resultados tendrán id (¿resultado?.id) entonces se invocará this.triggerAction(result.id)
Espero que funcione.
Gracias :)