【问题标题】:NodeJS: Error during posting an order using MongooseNodeJS:使用 Mongoose 发布订单时出错
【发布时间】:2022-01-29 10:32:42
【问题描述】:

我正在使用 NodeJS 和 Mongoose,这是一个购物车。我在发布订单时遇到错误。

这是错误输出:

Error: Order validation failed: user.userId: Path `user.userId` is required.
    at ValidationError.inspect (/opt/sourcecode/sandboxnodejs/node_modules/mongoose/lib/error/validation.js:48:26)
    at formatValue (node:internal/util/inspect:763:19)
    at inspect (node:internal/util/inspect:340:10)
    at formatWithOptionsInternal (node:internal/util/inspect:2006:40)
    at formatWithOptions (node:internal/util/inspect:1888:10)
    at console.value (node:internal/console/constructor:323:14)
    at console.log (node:internal/console/constructor:359:61)
    at /opt/sourcecode/sandboxnodejs/controllers/shop.js:108:29
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  errors: {
    'user.userId': ValidatorError: Path `user.userId` is required.
        at validate (/opt/sourcecode/sandboxnodejs/node_modules/mongoose/lib/schematype.js:1277:13)
        at /opt/sourcecode/sandboxnodejs/node_modules/mongoose/lib/schematype.js:1260:7
        at Array.forEach (<anonymous>)
        at ObjectId.SchemaType.doValidate (/opt/sourcecode/sandboxnodejs/node_modules/mongoose/lib/schematype.js:1210:14)
        at /opt/sourcecode/sandboxnodejs/node_modules/mongoose/lib/document.js:2700:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: [Object],
      kind: 'required',
      path: 'user.userId',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    }
  },
  _message: 'Order validation failed'
}

这是要点的链接:https://gist.github.com/hftamayo/bba430df7070bf6d141aeddcbb698716

控制器中触发这个错误的主要方法是这样的:

exports.postOrder = (req, res, next) => {
  req.user
    .populate("cart.items.productId")
    .then((user) => {
      const products = user.cart.items.map((i) => {
        return { quantity: i.quantity, product: { ...i.productId._doc } };
      });
      const order = new Order({
        user: {
          name: req.user.name,
          userId: req.user.userId
        },
        products: products,
      });
      return order.save();
    })
    .then((result) => {
      return req.user.clearCart();
    })
    .then(() => {
      res.redirect("/orders");
    })
    .catch((err) => console.log(err));
};

repo 是这个:https://github.com/hftamayo/sandboxnodejs/tree/chapt12_mongoose

涉及的其他组件有:

shop.js (controller)
user.js (model)
order.js (model)

我有几天的时间尝试调试它,我知道 userId 为空,但我不明白为什么,任何提示将不胜感激。

最好的祝福

【问题讨论】:

  • 也许你在没有验证的情况下调用你的方法?
  • 问题出在这个参数中:userId: req.user.userId 这是未定义的,所以,我把它改成_id,问题就解决了

标签: node.js mongoose mongoose-schema


【解决方案1】:

这个错误是关于一个未定义的参数(userId):

user: {
          name: req.user.name,
          userId: req.user.userId
        },

所以,这成功了:

user: {
          name: req.user.name,
          userId: req.user._id
        },

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-05
    • 2021-03-02
    • 2020-08-23
    • 1970-01-01
    • 2018-04-28
    • 2012-02-29
    • 2014-12-01
    • 1970-01-01
    相关资源
    最近更新 更多