http POST无法到达后端

时间:2019-12-13 15:47:23

标签: node.js angular

我正在尝试将电子邮件值传递给后端,但是它永远不会到达端点。我有相同的URL路径被调用,但是console.log("You've made it to the backend");从不输出。这是为什么?肯定会碰到有角度的服务,因为Made It输出到控制台没有问题。

角度服务

import { Injectable } from "@angular/core";
import { HttpClient, HttpParams } from "@angular/common/http";
import { Router } from "@angular/router";
import { PasswordReset } from "./password.model";

@Injectable({ providedIn: "root" })
export class PasswordResetService {
  constructor(private http: HttpClient, private router: Router) {}

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

app.js

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

console.log("You've made it to the backend");
let emailValue = req.body.email;

  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);

         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 + '<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: [emailValue ],
           Source: "'AWS Tutorial Series' <" + emailValue + ">'"

       };

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

              res.send(err);
              console.log(err);
          }
          else {
              res.send(data);
          }
      })

    }
      if (!user) {
        return res.status(401).json({
          message: "Auth failed"
        });
      }


    });


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

1 个答案:

答案 0 :(得分:1)

我的猜测是您没有订阅http请求。

请尝试此作为测试:

 this.http.post(
  `http://localhost:3000/api/users/passwordreset`,
  password
).subscribe(r=>console.log(r));
return null;