我正在开发一个无需用户登录即可订购食品的应用程序。他可以在订购过程中提供信息来订购。当我尝试创建命令时,我希望同时创建我的用户。我的问题是,在创建订单时显示错误消息,我不知道该如何解决此问题。模型-订单
const mongoose = require('mongoose');
const User = require('./User')
const Meal = require('./Meal')
const OrderSchema = new mongoose.Schema(
{
customer: {
type: mongoose.Schema.Types.ObjectId,
ref: User,
required: true
},
orderedmeal: {
type: mongoose.Schema.Types.ObjectId,
ref: Meal,
required: true
},
ordertotal: {
type: Number,
required: false,
default:0
},
dateOrder: String,
status: {
type: Boolean,
default: false
},
},
{ timestamps: true }
);
module.exports = mongoose.model("Order", OrderSchema);
控制器-创建订单
exports.createOrder = async (req, res) => {
const newOrder = new Order(req.body)
newOrder.save();
return res.status(200).json({
customer: newOrder.customer,
orderedmeal: newOrder.orderedmeal,
ordertotal: newOrder.ordertotal,
dateOrder: newOrder.dateOrder,
status : newOrder.status
})
}
订单
const [order, setOrder] = useState();
const handleSubmit = async (e) => {
e.preventDefault();
try {
const res = await axios.post(GET_ORDERS_URL, {
customer: {
firstname: firstname,
lastname: lastname,
numTel: numTel,
address: address,
},
orderedmeal: orderedmeal,
ordertotal: orderTotal,
dateOrder: dateOrder,
});
setOrder(res.data);
console.log("Order ", order);
} catch (error) {
console.log(error);
}
};
控制台上的结果
this.$__.validationError = new ValidationError(this);
^
ValidationError: Order validation failed: customer: Cast to ObjectId failed for value "{
firstname: 'djamila',
lastname: 'azouane',
numTel: '0777776602',
address: 'Hussein Dey'
}" (type Object) at path "customer" because of "BSONTypeError"
at model.Document.invalidate (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:3054:32)
at model.$set (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:1409:12)
at model.$set (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:1092:16)
at model.Document (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:166:12)
at model.Model (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\model.js:119:12)
at new model (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\model.js:4999:15)
at exports.createOrder (C:\Users\hp\Documents\booking\server\Controllers\order.js:9:20)
at Layer.handle [as handle_request] (C:\Users\hp\Documents\booking\server\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\hp\Documents\booking\server\node_modules\express\lib\router\route.js:144:13)
at next (C:\Users\hp\Documents\booking\server\node_modules\express\lib\router\route.js:138:14) {
errors: {
customer: CastError: Cast to ObjectId failed for value "{
firstname: 'djamila',
lastname: 'azouane',
numTel: '0777776602',
address: 'Hussein Dey'
}" (type Object) at path "customer" because of "BSONTypeError"
at ObjectId.cast (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\schema\objectid.js:248:11)
at ObjectId.SchemaType.applySetters (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\schematype.js:1201:12)
at model.$set (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:1367:22)
at model.$set (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:1092:16)
at model.Document (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:166:12)
at model.Model (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\model.js:119:12)
at new model (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\model.js:4999:15)
at exports.createOrder (C:\Users\hp\Documents\booking\server\Controllers\order.js:9:20)
at Layer.handle [as handle_request] (C:\Users\hp\Documents\booking\server\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\hp\Documents\booking\server\node_modules\express\lib\router\route.js:144:13) {
stringValue: '"{\n' +
" firstname: 'djamila',\n" +
" lastname: 'azouane',\n" +
" numTel: '0777776602',\n" +
" address: 'Hussein Dey'\n" +
'}"',
messageFormat: undefined,
kind: 'ObjectId',
value: {
firstname: 'djamila',
lastname: 'azouane',
numTel: '0777776602',
address: 'Hussein Dey'
},
path: 'customer',
reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
at new BSONTypeError (C:\Users\hp\Documents\booking\server\node_modules\bson\lib\error.js:41:28)
at new ObjectId (C:\Users\hp\Documents\booking\server\node_modules\bson\lib\objectid.js:67:23)
at castObjectId (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\cast\objectid.js:25:12)
at ObjectId.cast (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\schema\objectid.js:246:12)
at ObjectId.SchemaType.applySetters (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\schematype.js:1201:12)
at model.$set (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:1367:22)
at model.$set (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:1092:16)
at model.Document (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\document.js:166:12)
at model.Model (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\model.js:119:12)
at new model (C:\Users\hp\Documents\booking\server\node_modules\mongoose\lib\model.js:4999:15),
valueType: 'Object'
}
},
_message: 'Order validation failed'
}
[nodemon] app crashed - waiting for file changes before starting...
前端+导航控制台x1c 0d1x
谢谢你的好意,
1条答案
按热度按时间v09wglhw1#
在您的订单模式中,
customer
的类型为“ObjectId”,当您传递req以创建订单时,您传递的是customer对象,这是导致错误的原因。在createOrder
函数中,首先,您需要保存customer对象,然后使用其id创建订单。