So I tried to give null for the error message, but it shows
if(err){
res.status(403).json({
emailErr: err.errors.email.message || null,
userErr: err.errors.username.message || null,
});
TypeError: Cannot read property 'message' of undefined
Normally, JavaScript errors out if you try to access a property on undefined like in this lookup chain. You can use optional chaining to tell JavaScript to expect it:
if (err) {
res.status(403).json({
emailErr: err.errors.email?.message || null,
userErr: err.errors.username?.message || null,
});
}