我似乎无法在本地脚本角度响应的快速后端响应中找到标头

时间:2018-07-23 15:20:08

标签: express angular2-nativescript

我似乎无法在本地脚本角度响应的快速后端中找到任何标头,我以前有res.header,但没有什么不同。

Express后端:

router.post('/login', (req, res) => {
  User.findOne({
      email: req.body.email
    })
    .then((foundUser) => {
      if (!foundUser) res.send('Email Or Password Is Invalid');
      else {
        bcrypt.compare(req.body.password, foundUser.password) //compare hashed with db string
          .then((validPassword) => {
            if (!validPassword) res.send('Invalid Email Or Password').status(400);
            else {
              const token = jwt.sign({
                _userID: foundUser.userID
              }, config.get('jwtPrivateKey'));
              res.set({
                'content-type': 'application/json',
                'x-auth-token': token
              }).send('Logged In').status(200);
            }
          });

      }
    }).catch((err) => {
      res.send('Oops something went wrong').status(500);
    });
});

本地Http:

onLogin(){
    console.log(this.textScrap());
    this.registartion.Login(this.textScrap())
    .subscribe((response) => {
      console.log(response);
    },(err) => {
    console.log(err);
    },() => {
      // this.router.navigateByUrl()  Navigate to homePage when ready
    })
  }

然后我得到此响应,但不包含标题

{
JS:   "headers": {
JS:     "normalizedNames": {},
JS:     "lazyUpdate": null,
JS:     "headers": {}
JS:   },
JS:   "status": 200,
JS:   "statusText": "Unknown Error",
JS:   "url": null,
JS:   "ok": false,
JS:   "name": "HttpErrorResponse",
JS:   "message": "Http failure during parsing for (unknown url)",
JS:   "error": {}
JS: }

1 个答案:

答案 0 :(得分:0)

我在使用Angular6(和7)的NativeScript中遇到了类似的问题,结果是Angular HttpClient拒绝了状态码为200而不是204(无内容)的空响应。日志显示与您收到的消息相同。

即使您的服务器代码似乎在成功登录(“已登录”)时发送了非空响应,您仍可以尝试在Angular应用中添加拦截器 进一步检查错误。

在我的情况下,我添加了一个Interceptor,将其设置为null,以防HttpClient遇到错误并且状态代码指示成功响应:



    import { Observable, of } from 'rxjs';
    import { catchError } from 'rxjs/operators';
    import { Injectable } from '@angular/core';
    import {
      HttpInterceptor,
      HttpRequest,
      HttpHandler,
      HttpResponse,
      HttpEvent,
      HttpErrorResponse
    } from '@angular/common/http';

    @Injectable()
    export class EmptyResponseBodyErrorInterceptor implements HttpInterceptor {
        intercept(req: HttpRequest, next: HttpHandler):  Observable> {
        return next.handle(req).pipe(
            catchError((err: HttpErrorResponse, caught: Observable>) => {
                if (err.status >= 200 && err.status < 300) {
                    const res = new HttpResponse({
                        body: null,
                        headers: err.headers,
                        status: err.status,
                        statusText: err.statusText,
                        url: err.url
                      });
                      return of(res);
                }
                throw err;
            }
        ));
      }
    }


将其添加到您app.module.ts中的providers


    { provide: HTTP_INTERCEPTORS, useClass: EmptyResponseBodyErrorInterceptor, multi: true },

尽管在Chrome,Firefox或Safari中运行此问题,但我没有遇到相同的问题-仅在NativeScript(Android和iOS)中。