【问题标题】:Receiving Internal Server Error 500 when I call a method in the frontend (Ionic) that works correctly when I test it using postman to the backend当我在前端(离子)中调用一个方法时收到内部服务器错误 500,当我使用 postman 到后端对其进行测试时该方法正常工作
【发布时间】:2019-12-07 03:50:58
【问题描述】:

我创建了一个方法 (MongoDB),它采用优惠券的价格和用户的积分。一旦调用此方法,用户的积分就会从代金券的价格中扣除。我使用邮递员在后端测试了该方法,它工作正常。它返回用户并正确扣除积分。 我将该方法与前端 Ionic 连接,就像任何其他方法一样,但是每次调用它都会返回错误 500:内部服务器错误。

我尝试创建另一个名为 getPoints() 的方法(适用于后端/前端),它可以获取用户的积分,而不必以相同的方法访问用户的积分,因为我认为这可能是一个问题,但同样的事情发生了,调用 updateUserPoints() 再次返回错误 500。

updateUserPoints 方法:

module.exports.updateUserPoints = async function(req, res, next) {

var priceofvoucher = req.body.price;
var p=req.body.points;
p=p-priceofvoucher;

User.findByIdAndUpdate(
  {_id:req.decodedToken.user._id},
  {
    $set: { "points":  p}
  }
  ,
  { new: true }
).exec(function(err, updatedUser) {
  if (err) {
    return next(err);
  }
  if (!updatedUser) {
    return res
      .status(404)
      .json({ err: null, msg: "User not found.", data: null });
  }

  res.status(200).json({
    err: null,
    msg: "User was updated successfully.",
    data: updatedUser
  });
});
};

前端方法的服务:

updateUserPoints(p,voucher) {
var headers = new Headers();
headers.append("Authorization", "Bearer " + this.getAuthorizationToken());
headers.append("Content-Type", "application/json");
return this.http
  .post(environment.apiUrl + "/user/updateUserPoints", p + voucher,
  {
    headers
  })
  .map(res => res.json());
  }

页面的html文件(凭证部分):

<ion-list>
  <ion-item-sliding *ngFor="let voucher of vouchers | async">
    <ion-item>
      {{ voucher.offer }}
    </ion-item>
    <ion-item-options side="end">
      <button ion-button color="danger" (click)="removeVoucher(voucher)">
      <ion-icon name="trash"></ion-icon>
      Delete
    </button>
    </ion-item-options>
    <button (click)="subtraction(voucher)" ion-item>
      <ion-icon name="cart" item-start></ion-icon>
      Buy this Voucher
    </button>
  </ion-item-sliding>
</ion-list>

页面的打字稿文件:

subtraction(voucher) {
this.authService.getPoints().subscribe(data => {
this.p = data;
this.showToast(data.msg);
this.updateUserPoints(this.p , voucher.price);
this.loadVouchers();
});
}

getPoints() 返回 "data": 5 例如如果用户有 5 个点

【问题讨论】:

  • 你应该检查你的服务器的错误日志......
  • 我尝试在方法 getPoints() 和 updateUserPoints() 的开头打印一个打印语句,当我单击前端调用的按钮时调用了它们,只有打印语句在 getPoints() 被执行而 updateUserPoints() 没有所以我认为这可能是前端的问题吧?
  • 500: 内部服务器错误 === 服务器错误
  • 您可能在前端做的事情与邮递员不同,而且可能有问题,但无论哪种方式,服务器错误都不应返回,因为这意味着服务器上有未处理的事情。
  • 那些是访问日志,你需要错误日志

标签: node.js angular mongodb ionic-framework frontend


【解决方案1】:

问题似乎在这里:

.post(environment.apiUrl + "/user/updateUserPoints", p + voucher,

p+voucher 看起来不是一个有效的 json 对象,您可能会追求:

.post(environment.apiUrl + "/user/updateUserPoints", {points: p, price: voucher},

如果您想要一个属性点和价格分别对应于 p 和凭证值的实体,但是如果不知道实际实体的预期外观,就很难确定。

您还应该尝试弄清楚为什么您的服务器在无法解析发布请求的正文时会因错误而窒息,以便它可以向您发送正确的错误消息而不是 500

【讨论】:

  • 这是我要发送的正文:{ "points": 10, "price": 5} 当我使用您发送的新帖子时,它显示:"{"err": {"message":"路径 \"points\"","name":"CastError","stringValue":"\"NaN\"","kind" 的值 \"NaN\" 转换为数字失败: "number","value":null,"path":"points"},"msg":"500 内部服务器错误","data":null}"
  • 当您将 p 和凭证输入到 updateUserPoints 时,您必须在前端添加一些登录信息...现在消息告诉您积分不是数字
猜你喜欢
  • 1970-01-01
  • 2016-05-11
  • 2022-11-15
  • 2016-04-30
  • 1970-01-01
  • 1970-01-01
  • 2023-01-31
  • 2016-04-26
  • 1970-01-01
相关资源
最近更新 更多