将标头发送到客户端后无法设置标头-发送SES电子邮件

我正在尝试构建pw恢复解决方案,并且在app.post方法中添加了SES代码,但我一直得到Cannot set headers after they are sent to the client。错误被触发。

ses.sendRawEmail(params,function(err,data) {
         if(err) {
      res.send(err);
         }

我在User.findOne认为可行之前添加了params代码,但是我猜想它是在到达Api端点之前正在谈论标头。除非我弄错了,否则在那种情况下这将无法实现。如何使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,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.");
  }
});
yan_xiao_yu11 回答:将标头发送到客户端后无法设置标头-发送SES电子邮件

问题是您多次使用res.send。保证一切,一切都会很好

本文链接:https://www.f2er.com/2946544.html

大家都在问