【问题标题】:Only part of the response is retrieved at the ionic 4 front-end在 ionic 4 前端仅检索到部分响应
【发布时间】:2019-07-26 05:12:06
【问题描述】:

我正在使用令牌从 ionic 向 NodeJS 服务器发送一个 http GET 请求。我收到一条消息,表明数据已成功检索。但是,响应的数据属性不会发送到前端,并且当我尝试在控制台中记录它时被分配给 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
    );

这里是中间件isAuthenticated:

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

【问题讨论】:

    标签: node.js angular asynchronous ionic-framework middleware


    【解决方案1】:

    你从后端得到什么?错误还是数据? 首先检查您的请求是否获得了后端 api。 其次,我看到您检查了中间件中的小写授权;也许这就是事情出错的地方。 第三,可能用户根本没有积分,默认情况下,空字段从响应中消除。

    【讨论】:

    • 我从后端收到此消息{err: null, msg: "Current user points retrieved successfully."} 它与 200 ok 响应完全相同,但缺少数据属性。并且用户模型确实有一个 points 字段。我会将架构添加到我的问题中
    • 我只是添加架构
    • 我认为用户对象是 null 或者在代码的最后部分有一些东西
    • 在响应 ionic 之前只记录用户对象
    • 我看不到积分字段。在 res.status 之前记录用户对象和 user.points
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2017-07-18
    • 2019-04-06
    • 1970-01-01
    • 2022-01-08
    • 2016-04-27
    • 1970-01-01
    相关资源
    最近更新 更多