I have small problem, but I don’t know what I doing wrong. I have function like below. In .catch alert working ok and I see error in browser, but I have problem with this same error in console.log <- I don’t see anything 😦 Basically, I wanted to pass error to another function like notify(error) but I don’t see anything.
const signIn = (e) => {
e.preventDefault();
signInWithEmailAndPassword(auth, email, password)
.then(() => {
navigate("/");
})
.catch((error) => {
// notify(error)
console.log("error: ", error);
alert(error);
});
};
I try doing try catch and async await but I have the same problem:
async function signIn(e) {
try {
e.preventDefault();
await signInWithEmailAndPassword(auth, email, password).then(() => {
navigate("/");
});
} catch (error) {
// notify(error);
console.error("error: ", error);
alert(err);
}
}
2
Answers
You can not call error object directly in console.log as js has some issues parsing error as a JSON object. Instead, use
error.message
orerror.toString()
innotify()
function. Error object has breaks that cause issues when parsing to a JSON object.Check below code, only
error.message
that is the first line (key : value pair) of the object is accessible. Notice thatJSON.parse(error)
gives an error because of this.The issue you’re facing could be related to the error object being passed to the console.log statement. Sometimes, the error object may not be displayed directly in the console, depending on its structure.
To ensure that the error is logged properly, you can try using console.error instead of console.log:
Additionally, if you want to pass the error to another function like notify, you can un-comment the notify(error) line and make sure that the notify function is correctly implemented and able to handle the error object.