node.js - 尽管捕获了 Angular 2 中的错误,但仍收到 400 Bad Request

标签 node.js angular typescript http-status-code-400 bad-request

我设计了一个登录页面,当我输入正确的登录名和密码时,登录成功;当我输入正确的错误用户名或密码时,登录不会发生。但是,我收到此错误:

POST http://localhost:3003/login/authenticate 400 (Bad Request)

ERROR HttpErrorResponse {headers: HttpHeaders, status: 400, statusText: "Bad Request", url: "http://localhost:3003/login/authenticate", ok: false, …}

一切正常,但是我在控制台中收到错误。像这样:

dom

我希望 400 bad request 错误不会出现在控制台中。如何做到这一点?

login.component.ts

login(data) {

    console.log("Inside Login");

    this.authenticateObj = {
      username: data.username,
      password: data.password
    }


    this.http.post("http://localhost:3003/login/authenticate", 
      this.authenticateObj)
      .map(Response => Response)
        .catch((err) => {
          console.log("err =", err)
         alert('Login Failed. Username or Password 
       is incorrect');
          return Observable.throw(err);
      })
      .subscribe((res: Response) => {
        console.log("Inside login authenticate subscribe");

        this.info = res;
        if (this.info.message == 'Login Successful.') {


          console.log("test after login = ", this.info);


          if (localStorage.getItem('username') && 
          localStorage.getItem('token')) {
             alert('Login Successful');
            this.router.navigate(['/file-upload/wsdl']);
          } else {
            this.notification.Error('Unauthorized');
          }
        }
        if (this.info.message == 'error') {
          alert('Login Failed');
        }
        else if (this.info.status == 400) {
          alert('Login Failed');
        }
      })


  }

login.controller.js

function authenticateUser(req, res, next) {

console.log("Inside authenticateUser = ", req.body)

    LoginService.authenticate(req,req.body)
        .then(function (token) {

            if (token) {
                res.setHeader("authorization",token.token);
                res.send({
                  message: 'Login Successful.',   

                    response: token
                });
            } else if(res.message == 'Username or Password is 
            incorrect'){ 

                res.status(401).send({
                    message: 'Unauthorized. '
                });
            }
            else { 
                console.log("inside controller, else res.status-400");  
                res.status(400).send({
                    message: 'Username or password is incorrect'
                });
            }
        })
        .catch(function (err) {
            console.log("inside controller, catch res.status 400")
            // res.status(400).send(err);

            res.status(400).send({
                message: 'Username or password is incorrect'
            });

        });
}

最佳答案

为了正确处理来自服务器的错误,您必须在 subcribe() 中捕获它们。 Observable的方法返回者 http.post来自 Rxjs:

this.http.post("http://localhost:3003/login/authenticate", this.authenticateObj)
    .subscribe(
        (res: Response) => {
            // code when no error...
        }, 
        err => {
            // error handling...
        },
        () => {
            // finally...
        }
    ); 

关于node.js - 尽管捕获了 Angular 2 中的错误,但仍收到 400 Bad Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54034556/

相关文章:

angular - 如何使用 *ngIf 指令更改图像?

node.js - 运行空白 typescript 项目

typescript - 为什么 A | B允许两者结合,我该如何防止呢?

javascript - NodeJs Crypto - MD5 哈希不正确

node.js - 有关未读消息的通知(node.js + sockets.io)

javascript - 在 root 上创建目录不能通过 npm 工作,但可以直接运行 Node

angular - 在 CanActivate 期间显示加载指示器

node.js - 很难启动 Angular CLI 服务器

javascript - 使用默认值解构空对象时 typescript 类型错误?

javascript - 如何用node.js正确输出引号