我有一个MERN应用程序,旨在收集表单数据并将其发送到服务器,nodemailer会将邮件发送给收件人。一切正常,在前端执行错误。我已经在我的Express应用程序中设置了validator
,它正在捕获任何预期的错误,但仅在邮递员中。说到React,我只会得到
POST http://localhost:5000/send 400 (Bad Request)
Error: Request failed with status code 400
at createError (createError.js:17)
at settle (settle.js:19)
at XMLHttpRequest.handleLoad (xhr.js:78)
如果有任何错误发生,我有errors
对象会被填充并发送...
app.post('/send', (req, res) => {
const {
errors,
isValid
} = validatedInput(req.body);
// Check Validation
if (!isValid) {
return res.status(400).json(errors);
}
const output = `
<h1>New Adverse Event reported!</h1>
<ul style="list-style:none;">
<li>Name: <h3>${req.body.name}</h3></li>
<li>Lastname: <h3>${req.body.lastname}</h3></li>
<li>Location: <h3>${req.body.location}</h3></li>
<li>Phone: <h3>${req.body.phone}</h3></li>
<li>Phone: <h3>${req.body.email}</h3></li>
<li>Date: <h3>${req.body.date}</h3></li>
<li>Drug: <h3>${req.body.drug}</h3> </li>
<li>Batch number: <h3>${req.body.batch}</h3></li>
<li>Adverse Event Outcome: <h3>${req.body.outcome}</h3></li>
</ul>
<p>
Adverse Event details: <br>
${req.body.details}
</p>
`
let transporter = nodemailer.createTransport({
host: process.env.SMTP,
port: process.SMTP_PORT,
secure: true, // true for 465, false for other ports
auth: {
user: process.env.AUTH_USER, // generated ethereal user
pass: process.env.AUTH_PASS // generated ethereal password
},
tls: {
rejectUnauthorized: false
}
});
// setup email data with unicode symbols
let mailOptions = {
from: '"Adverse Event Pharma" <foo@example.com>', // sender address
to: '',
subject: `New AE has been reported for ${req.body.drug}`, // Subject line
html: output // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
res.status(200).json({
'msg': 'Message has been sent'
})
});
})
我的问题是如何在前端获取此错误并显示它们?