I encountered a problem where everything is properly working on pc, but on mobile phone, it is a different story. When the request is done on pc, it alerts the user on the screen that: "Name is required." and so on (I did not fill name or other properties in purpose). However, when this time I try it on mobile phone, it throws me an error that Cannot read properties of undefined (reading 'data') and gives reference to the line const data = await err.response.data;
There is a code snippet:
try {
const config = {
headers: {
"Context-Type": "application/json",
},
};
const res = await axios.post(
USERS_PATH,
{
name,
surname,
email,
password,
repeatedPassword,
},
config
);
console.log(res.data);
} catch (err) {
const data = await err.response.data;
userCtx.updateAlertMessage(Object.values(data).flat());
}
It should crush regardless of the platform. To fix it, check if the response is null before accessing the data. You can do it using the ? null checker.
Modified line:
const data = await err.response?.data;