NodeJS:使用猫鼬发布订单时出错
NodeJS: Error during posting an order using Mongoose
我正在使用 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'
}
这是 link 要点: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));
};
回购是这个:https://github.com/hftamayo/sandboxnodejs/tree/chapt12_mongoose
涉及的其他组件是:
shop.js (controller)
user.js (model)
order.js (model)
我花了好几天时间调试它,我知道 userId 为 null 但我不明白为什么,任何提示都将不胜感激。
祝福
该错误与未定义的参数 (userId) 有关:
user: {
name: req.user.name,
userId: req.user.userId
},
所以,这成功了:
user: {
name: req.user.name,
userId: req.user._id
},
我正在使用 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'
}
这是 link 要点: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));
};
回购是这个:https://github.com/hftamayo/sandboxnodejs/tree/chapt12_mongoose
涉及的其他组件是:
shop.js (controller)
user.js (model)
order.js (model)
我花了好几天时间调试它,我知道 userId 为 null 但我不明白为什么,任何提示都将不胜感激。
祝福
该错误与未定义的参数 (userId) 有关:
user: {
name: req.user.name,
userId: req.user.userId
},
所以,这成功了:
user: {
name: req.user.name,
userId: req.user._id
},