I have the following schemas:
我有以下架构:
// ingredient
var ingredSchema = new Schema({
name: String,
cost: Number
});
// order
var orderSchema = new Schema({
cusName: String,
ingredients: [{type: Schema.Types.ObjectId, ref: 'Ingredient'}]
});
// create model
var Ingredient = mongoose.model('Ingredient', ingredSchema);
var Order = mongoose.model('Order', orderSchema);
I have already saved a bunch ingredients in a collection ingredients
and have a UI where users choose a set of ingredients for their burgers. I then try to save an order for a burger in another collection orders
within the same database burgers
like this:
我已经在一个收集成分中保存了一堆成分,并有一个用户界面,用户可以选择一套汉堡食材。然后我尝试在同一个数据库汉堡中的另一个集合命令中保存汉堡的订单,如下所示:
// get order info from the form
var newOrder = new Order({ cusName: req.body.name,
ingredients: req.body.ingredients });
newOrder.save(function(err) {
if (err)
return console.log('Could not save your new order', err);
res.redirect('/order');
});
The call to save an order generates the following error:
保存订单的调用会生成以下错误:
{ message: Cast to ObjectId failed for value xxx at path 'ingredients',
name: 'CastError',
type: ObjectId,
value: xxx,
path: 'ingredients' }
I use mongoose version 3.6.11. Please help me hack this.
我使用mongoose版本3.6.11。请帮我解决这个问题。
PS: req.body.ingredients is an array built from checkboxes.
PS:req.body.ingredients是一个由复选框构建的数组。
1 个解决方案
#1
22
There are 2 possible problems with your code right now:
您的代码现在有两个可能的问题:
1. req.body.ingredients
will not be an array of ObjectId
s, and mongoose wants it alright (I doubt of this one).
1. req.body.ingredients不会是一个ObjectIds数组,而且mongoose希望它没问题(我怀疑这一点)。
You should cast every ingredient to ObjectId
first. Supposing req.body.ingredients
is array, then you would do something like this:
您应该首先将每个成分转换为ObjectId。假设req.body.ingredients是数组,那么你会做这样的事情:
var casted = req.body.ingredients.map(function( ingredient ) {
return mongoose.Types.ObjectId(ingredient);
});
I did not tested this, see if it'll work for you.
我没有对此进行测试,看看它是否适合你。
2. Mongoose is trying to cast your ingredients, but one of them is not a valid ObjectId
2.猫鼬正试图施放你的成分,但其中一个不是有效的ObjectId
ObjectId
should be composed of 24 hex chars, check whether you're passing values like this to Mongoose.
ObjectId应该由24个十六进制字符组成,检查你是否将这样的值传递给Mongoose。
Please, post the result if one of them work for you :)
如果其中一个为你工作,请发布结果:)
#1
22
There are 2 possible problems with your code right now:
您的代码现在有两个可能的问题:
1. req.body.ingredients
will not be an array of ObjectId
s, and mongoose wants it alright (I doubt of this one).
1. req.body.ingredients不会是一个ObjectIds数组,而且mongoose希望它没问题(我怀疑这一点)。
You should cast every ingredient to ObjectId
first. Supposing req.body.ingredients
is array, then you would do something like this:
您应该首先将每个成分转换为ObjectId。假设req.body.ingredients是数组,那么你会做这样的事情:
var casted = req.body.ingredients.map(function( ingredient ) {
return mongoose.Types.ObjectId(ingredient);
});
I did not tested this, see if it'll work for you.
我没有对此进行测试,看看它是否适合你。
2. Mongoose is trying to cast your ingredients, but one of them is not a valid ObjectId
2.猫鼬正试图施放你的成分,但其中一个不是有效的ObjectId
ObjectId
should be composed of 24 hex chars, check whether you're passing values like this to Mongoose.
ObjectId应该由24个十六进制字符组成,检查你是否将这样的值传递给Mongoose。
Please, post the result if one of them work for you :)
如果其中一个为你工作,请发布结果:)