Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 调用前端(Ionic)中的方法时收到内部服务器错误500,该方法在使用postman对后端进行测试时正常工作_Node.js_Angular_Mongodb_Ionic Framework_Frontend - Fatal编程技术网

Node.js 调用前端(Ionic)中的方法时收到内部服务器错误500,该方法在使用postman对后端进行测试时正常工作

Node.js 调用前端(Ionic)中的方法时收到内部服务器错误500,该方法在使用postman对后端进行测试时正常工作,node.js,angular,mongodb,ionic-framework,frontend,Node.js,Angular,Mongodb,Ionic Framework,Frontend,我创建了一个方法(MongoDB),它接受凭证的价格和用户的积分。一旦调用此方法,用户的分数将由凭证的价格扣除。我使用postman在后端测试了该方法,效果很好。它返回用户并正确扣除分数。 我将该方法与前端Ionic连接,就像其他任何方法一样,但是每次调用它时,它都返回错误500:内部服务器错误 我尝试创建另一个名为getPoints()的方法(它在后端/前端工作),该方法获取用户的点,而不必在同一方法中访问用户的点,因为我认为这可能是一个问题,但发生了同样的情况,再次调用updateUserP

我创建了一个方法(MongoDB),它接受凭证的价格和用户的积分。一旦调用此方法,用户的分数将由凭证的价格扣除。我使用postman在后端测试了该方法,效果很好。它返回用户并正确扣除分数。 我将该方法与前端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文件(凭证部分):


getPoints()返回“数据”:5例如,如果用户有5个点,则问题出现在此处:

.post(environment.apiUrl + "/user/updateUserPoints", p + voucher,
p+凭证
看起来不是一个有效的json对象,您可能需要:

.post(environment.apiUrl + "/user/updateUserPoints", {points: p, price: voucher},
如果您想要一个实体,其属性、点数和价格分别对应于p和凭证的值,但如果不知道实际实体的外观,则很难确定


您还应该尝试找出,当服务器无法解析post请求的正文时,为什么会出现错误,以便它可以向您发送正确的错误消息,而不是500条错误消息。问题如下:

.post(environment.apiUrl + "/user/updateUserPoints", p + voucher,
p+凭证
看起来不是一个有效的json对象,您可能需要:

.post(environment.apiUrl + "/user/updateUserPoints", {points: p, price: voucher},
如果您想要一个实体,其属性、点数和价格分别对应于p和凭证的值,但如果不知道实际实体的外观,则很难确定


您还应该尝试找出当您的服务器无法解析post请求的正文时出现错误的原因,以便它可以向您发送正确的错误消息,而不是500

您应该检查服务器的错误日志……我尝试在方法getPoints()和updateUserPoints()的开头打印一条print语句当我单击前端中调用subtract的按钮时,在updateUserPoints()时,只执行getPoints()中的print语句不是吗,我认为前端可能有问题,对吗?500:内部服务器错误===服务器错误你可能在前端做了一些与邮递员不同的事情,可能是出了问题,但无论如何,服务器错误都不应该返回,因为这意味着服务器上有未处理的内容。这些是访问日志,您需要错误日志您应该检查服务器的错误日志…我尝试在方法getPoints()和updateUserPoints()的开头打印print语句当我单击前端中调用subtract的按钮时,在updateUserPoints()时,只执行getPoints()中的print语句不是吗,我认为前端可能有问题,对吗?500:内部服务器错误===服务器错误你可能在前端做了一些与邮递员不同的事情,可能是出了问题,但无论如何,服务器错误都不应该返回,因为这意味着服务器上有未处理的内容。这些是访问日志,您需要错误日志这是我试图发送的正文:{“points”:10,“price”:5}当我使用您发送的新帖子时,它显示:{“err”:{“message”:“Cast to number失败,因为路径上的值\“NaN\”\“points\”,“name”:“CastError”,“stringValue”:“NaN\”,“kind”:“number”,“value”:null,“path”:“points”},“msg”:“500内部服务器错误”,“data”:null}“当您将p和凭证输入到updateUserPoints时,您必须在前端添加一些日志记录。。。现在这条消息告诉你点数不是一个数字,这是我试图发送的正文:{“点数”:10,“价格”:5}当我使用你发送的新帖子时,它向我显示:{“错误”:{“消息”:“在路径\“点数\”,“名称”:“CastError”,“stringValue”:“NaN\”,“种类”:“数字”,“值”:null,“路径”:“点数”msg“:“500内部服务器错误”,“数据”:null}”当您将p和凭证输入到updateUserPoints时,您必须在前端添加一些日志记录。。。现在信息告诉您,点数不是一个数字