[英]How to save an array of objectID in mongoose and add fields?
I am trying to save in an Array the following... 我正在尝试将以下内容保存在数组中...
I send this by POST 我通过POST发送
req.body.products = req.body.products =
{id: "5936460d2fabd233f0ddaca3", buy: 30, cant:20},
{id: "5936460d2fabd233f2ddace6", buy: 25, cant:14}
That id is the ObjectId of an existing product 该id是现有产品的ObjectId
This is my controller 这是我的控制器
let facturaProv = new provBill({
id_store: res.locals.store.id,
name: req.body.name,
category: req.body.category,
products: req.body.products
});
facturaProv.save(function(err) {
if (!err) res.send('OK')
else res.send("error")
}
});
This is my Schema 这是我的架构
provSchema: new Schema({
id_store: { type: mongoose.Schema.Types.ObjectId, ref: 'store' },
category:String,
name: String,
products: [{ type: mongoose.Schema.Types.ObjectId, ref: 'product' }]
})
How should I modify my schema to save the "buy" and "cant" fields? 我应该如何修改架构以保存“购买”和“不能”字段?
I tried the following but it does not work: 我尝试了以下操作,但不起作用:
products: [{ type: mongoose.Schema.Types.ObjectId, ref: 'product', buy: Number, cant:Number }]
Thank you for reading and for helping me. 感谢您的阅读和帮助。
Modify the schema as following: 修改架构,如下所示:
products: [
{
id : {type: mongoose.Schema.Types.ObjectId, ref: 'product'},
buy: Number,
cant: Number
}
]
This is not a correct JSON object {id: "5936460d2fabd233f0ddaca3", buy: 30, cant:20}, {id: "5936460d2fabd233f2ddace6", buy: 25, cant:14}
这不是正确的JSON对象{id: "5936460d2fabd233f0ddaca3", buy: 30, cant:20}, {id: "5936460d2fabd233f2ddace6", buy: 25, cant:14}
Note the "" 注意“”
{"id": "5936460d2fabd233f0ddaca3", "buy": 30, "cant":20}, {"id": "5936460d2fabd233f2ddace6", "buy": 25, "cant":14}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.