切换映射第二个HTTP调用未返回值-标头错误

时间:2019-12-15 16:55:45

标签: node.js angular express

我正在尝试创建用于pw恢复的一次性URL链接系统。为了使其正常工作,我首先向用户发送电子邮件以重置密码。我想做的是将在后端创建的URL返回到emailPasswordRecoveryAndStoreUrl(email: string),以便我可以将该URL发布到后端,然后稍后检查它是否存在,如果链接已链接,则将用户重定向到链接已过期的页面被再次点击。

当前

    return res.status(200).json({
        message: "url retrieved",
        url: generatedUrl
      })

提供了Cannot set headers after they are sent to the client,但是我不知道如何在不执行res.status(200)的情况下获取返回值,以便我可以在下一个http调用中发布。

感谢您的帮助!

password-reset.service.ts


         sendEmail(email: string) {
        const password: PasswordReset = {
          email: email
        };
        console.log("Made It");
        return this.http.post<{
          url: string;
        }>(`http://localhost:3000/api/users/passwordreset`, password);
      }

      emailPasswordRecoveryAndStoreUrl(email: string) {
        const password: PasswordReset = {
          email: email
        };
        let url: string;
        return this.sendEmail(email).pipe(
          switchMap(value => {
            (url = value.url), console.log(url);
            return this.http.post(
              `http://localhost:3000/api/users/passwordurl`,
              url
            );
          })
        );
      }

app.js



app.post("/api/users/passwordreset/", function(req, res) {

    if (req.body.email !== undefined) {
      User.findOne({ email: req.body.email })
      .then(user => {
        if(user){
          console.log("fetchedUser");
          console.log(user._id);
          var payload = {
            id: user._id,
            email: user.email
          };

           var secret = user.password + "-" + user.passwordCreated;
           console.log("THE SECRET IS: " + secret);
           var token = jwt.sign(payload, secret);
           console.log("payload");
           console.log(payload);


           let generatedUrl = "http://localhost:3000/resetpassword/" + payload.id + '/' + token


           var ses_mail = "From: 'Auction Site' <" + user.email+ ">\n";
           ses_mail = ses_mail + "To: " +user.email + "\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 + '<a href="http://localhost:3000/resetpassword/' + payload.id + '/' + token + '">Click here to reset password</a>';
         //  /:id/:token

           var params = {
             RawMessage: { Data: new Buffer.from(ses_mail) },
             Destinations: [user.email ],
             Source: "'AWS Tutorial Series' <" + user.email + ">'"

         };

           ses.sendRawEmail(params, function(err, data) {
            if(err) {

                res.send(err);
                console.log(err);
            }
            else {
                res.send(data);
            }
        })
        return res.status(200).json({
          message: "url retrieved",
          url: generatedUrl
        })
      }
        if (!user) {
          return res.status(401).json({
            message: "Auth failed"
          });
        }


      });


    } else {
      res.send("Email address is missing.");
    }
  });



app.post("/api/users/passwordurl", function(req, res) {

console.log("URL CALL");

});

0 个答案:

没有答案