Express-Validator Invoces Catch Block des ControllersJavaScript

Javascript-Forum
Anonymous
 Express-Validator Invoces Catch Block des Controllers

Post by Anonymous »

Ich verwende Express-Validator, um die E-Mails von req.body zu überprüfen. Die Validierung funktioniert einwandfrei, aber wenn ich ordnungsgemäße E -Mail (Fehlerarray des Express -Validators ist leer) eingreift, fangen Block (ERRORHALTLER - "Netzwerkfehler") des Controllers ein. Wenn ich die Validierungslogik von Controller entfernen, funktioniert alles einwandfrei. Warum? < /P>
Fehlerhandler: < /p>

Code: Select all

    const errorHandler = (error, req, res, next) => {
if (error instanceof AppError) {
return res.status(error.statusCode).json({
message: error.message,
statusCode: error.statusCode,
});
}

return res.status(500).send({ message: "Network error." });
};
module.exports = errorHandler;
< /code>
Controller: < /p>
    exports.sendForgotPasswordEmail = async (req, res, next) => {
try {
const result = validationResult(req);
if (!result.isEmpty()) {
throw new AppError(result.array()[0].msg, 400);
}

const data = matchedData(req);
const { email } = data;

const emailHash = bcrypt.hashSync(email, 8);
const token =
Math.random().toString(36).substring(7) +
Math.round(new Date().getTime() / 1000) +
emailHash;
const user = await User.findOne({ email });
if (!user) {
throw new AppError("User doesn't exists.", 400);
}
await ResetPassword.updateOne(
{
email,
},
{
$set: {
email,
token,
siteUrl: process.env.SITE_URL,
timestamp: Date.now(),
},
},
{
upsert: true,
}
);
res.send({
message:
"Reset Password Link sent to the registered email successfully.",
});

} catch (error) {
return next(error);
}
};
< /code>
Route: < /p>
    router.post(
"/forgot-password",
body("email")
.trim()
.notEmpty()
.withMessage("Email can not be empty")
.isEmail()
.withMessage("Email must contain @ character."),
sendForgotPasswordEmail
);

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post