我正在尝试构建一个密码恢复解决方案,并在 app.post 方法中添加了 SES 代码,但我不断收到在将 header 发送到客户端后无法设置 header
。正在触发错误。
ses.sendRawEmail(params, function(err, data) {
if(err) {
res.send(err);
}
我在 User.findOne 之前添加了参数代码,认为这会起作用,但我猜测它在到达 Api 端点之前正在谈论 header 。在这种情况下,除非我弄错了,否则这不可能实现。如何让 AWS SES 代码在 app.post 中正常运行?
app.post("/api/users/passwordreset", function(req, res) {
console.log(req.body.email);
let emailValue = req.body.email;
if (req.body.email !== undefined) {
// TODO: Using email, find user from your database.
var ses_mail = "From: 'Auction Site' <" + emailValue+ ">\n";
ses_mail = ses_mail + "To: " +emailValue + "\n";
ses_mail = ses_mail + "Subject: Password Reset Request\n";
ses_mail = ses_mail + "MIME-Version: 1.0\n";
ses_mail = ses_mail + "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n";
ses_mail = ses_mail + "--NextPart\n";
ses_mail = ses_mail + "Content-Type: text/html; charset=us-ascii\n\n";
ses_mail = ses_mail + "Click here to reset password." + "\n\n" + 'http://localhost:3000/resetpassword';
var params = {
RawMessage: { Data: new Buffer.from(ses_mail) },
Destinations: [emailValue ],
Source: "'AWS Tutorial Series' <" + emailValue + ">'"
};
User.findOne({ email: req.body.email })
.then(user => {
if(user){
console.log("fetchedUser");
console.log(user);
var payload = {
id: user.id, // User ID from database
email: user.email
};
var secret = user.password + "-" + user.creationDate;
var token = jwt.sign(payload, secret);
console.log("payload");
console.log(token);
ses.sendRawEmail(params, function(err, data) {
if(err) {
res.send(err);
}
else {
res.send(data);
}
});
res.send(
'<a href="/resetpassword/' +
payload.id +
"/" +
token +
'">Reset password</a>'
);
}
if (!user) {
return res.status(401).json({
message: "Auth failed"
});
}
});
// TODO: Make this a one-time-use token by using the user's
// current password hash from the database, and combine it
// with the user's created date to make a very unique secret key!
// For example:
// var secret = user.password + ‘-' + user.created.getTime();
// TODO: Send email containing link to reset password.
// In our case, will just return a link to click.
} else {
res.send("Email address is missing.");
}
});
最佳答案
问题是您多次使用res.send
。做出一切 promise ,一切都会顺利
关于node.js - 发送到客户端后无法设置 header - 发送 SES 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59274156/