【问题标题】:ExpressJs returns empty res.jsonExpressJs 返回空 res.json
【发布时间】:2015-02-25 07:22:02
【问题描述】:

在并行中间件的“pre”中验证猫鼬模式时

schema.pre('save', true, function (next, done) {
  if(...) {
     next(new Error('Some error message'));
  }
  next();
});

我返回一个错误,它在回调函数中可用:

model.save({},{}, function(err) {
  res.json(400, err);
  console.log(err)// I see in the console: [Error: 'Some error message']
})

但是当我这样做时

res.json(400, err);

我得到一个空响应

{} No properties

这是什么原因?

【问题讨论】:

    标签: json node.js express


    【解决方案1】:

    JSON 不能对错误进行字符串化。您需要使用其他东西来发送错误。

    也许只是res.send(err.message),或类似的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-13
      • 2020-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      相关资源
      最近更新 更多