繁体   English   中英

在ionic 4前端仅检索到部分响应

[英]Only part of the response is retrieved at the ionic 4 front-end

我正在使用令牌将来自ionic的http GET请求发送到NodeJS服务器。 我收到一条消息,提示已成功检索数据。 但是,当我尝试将其记录在控制台中时,响应的数据属性不会发送到前端,而是分配给undefined。 这是我的离子请求的代码:

 getPoints() {
    var headers = new Headers();
    headers.append("Authorization", "Bearer " + this.getAuthorizationToken());
    headers.append("Content-Type", "application/json");
    console.log(this.getAuthorizationToken());
   return this.http
      .get(environment.apiUrl + "/user/getCurrentPoints", {
        headers
      })
      .map(res => res.json());
  }

这是离子组件内部的代码,我在构造函数中调用GET方法:

 constructor(
    ease: RoundProgressEase,
    private _router: Router,
    private formB: FormBuilder,
    private _authService: AuthService
  ) {
    for (let prop in ease) {
      if (prop.toLowerCase().indexOf("ease") > -1) {
        this.animations.push(prop);
      }
    }
    this._authService.getPoints().subscribe((res: any) => {
      console.log("data points", res);
    });
  }

这也是后端的端点:

    router.get(
      "/user/getCurrentPoints",
      isAuthenticated,
      userCtrl.getCurrentPoints
    );

这是经过身份验证的中间件:

  var isAuthenticated = function(req, res, next) {
      var token = req.headers["authorization"];
      console.log(req.header);
      token = req.headers.authorization.split("Bearer ")[1];
      if (!token) {
        return res.status(401).json({
          error: null,
          msg: "You have to login first before you can access your lists.",
          data: null
        });
      }
      jwt.verify(token, req.app.get("secret"), function(err, decodedToken) {
        if (err) {
          return res.status(401).json({
            error: err,
            msg: "Login timed out, please login again.",
            data: null
          });
        }
        req.decodedToken = decodedToken;
        console.log(req.decodedToken);
        next();
      });
    };

最后,这是userCtrl.getCurrentPoints控制器方法:

module.exports.getCurrentPoints = function(req, res, next) {
  console.log("accessed get current points");
  if (!Validations.isString(req.decodedToken.user.username)) {
    return res.status(422).json({
      err: null,
      msg: "type parameter must be a valid String.",
      data: null
    });
  }
  User.find({ username: req.decodedToken.user.username }).exec(function(
    err,
    user
  ) {
    if (err) {

      return next(err);
    }
    res.status(200).json({
      err: null,
      msg: "Current user points retrieved successfully.",
      data: user.points
    });
  });
};

这是我得到的回应

{err: null, msg: "Current user points retrieved successfully."}

用户架构

const mongoose = require('mongoose');
var Voucher = mongoose.Schema.Types.Voucher;

const userSchema = mongoose.Schema({
  username: {
    type: String,
    required: true,
    trim: true,
    lowercase: true

  },
  password: {
    type: String,
    required: true,
    trim: true
  },
  name: {
    type: String,
    required: true,
    trim: true
  },
  region: {
    type: String,
    required: true,
    trim: true,
  },
  building: {
    type: Number,
    required: true,
    min: 0
  },
  points: {
    type: Number,
    required: false,
    min: 0
  },
  vouchers: {
    type: [Voucher],
    status:[String],
    required: false,
  },

  createdAt: {
    type: Date,
    default: Date.now
  },
  updatedAt: Date
});

mongoose.model('User', userSchema);

您从后端得到什么? 错误或数据? 首先检查您的请求是否获取后端api。 其次,我看到您检查了中间件中的小写授权。 也许那是出问题的地方。 第三,用户根本没有任何点,默认情况下,从响应中删除空字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM