I have been trying to develop an application using react js, this error is coming.
The line where this error occurs is:
else{
history.push({
pathname:"/fullpage-error",
state:{details:e.response.data.errors[0]} //this is the main line of error
});
}
}
};
export default globalAPIFunction;
CodePudding user response:
What is the value of variable "e" at the line where the error occurs?
The variable e doesn't have the property "response"(It will be undefined), So it is not able to access the "data" property of undefined.
You can add a null pointer check before your statement if the variable e is a dynamic variable which you receiving from an API service or any third-party application
else{
if(e.response)
history.push({
pathname:"/fullpage-error",
state:{details:e.response.data.errors[0]} //this is the main line of error
});
}
}
};
export default globalAPIFunction;
Read more about "Type Error": https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError
CodePudding user response:
It is throwing error because the 'e' object is not set yet. you need to check first if it is defined or not do like this
else{
history.push({
pathname:"/fullpage-error",
state:{details:e.response ? e.response.data.errors[0] : ''} //this is the main line of error
});
}
}
};
export default globalAPIFunction;