如何在我的架构中向产品{type: mongoose.Schema.Types.ObjectId, ref: 'Product'}
发出帖子请求,这就是我的架构的样子
const CategorySchema = mongoose.Schema({
name: {
type: String,
required: true
},
img_url:{
type:String,
required: true
},
product: [
{type: mongoose.Schema.Types.ObjectId, ref: 'Product'}
]
})
但我不知道如何发布到产品阵列?
router.post('/',(req,res)=>{
const newCategory = newCategory()
category.name = req.body.name;
category.img_url = req.body.img_url;
Category.save(newCategory,(err,category)=>{
if (err) {
console.log(err);
}else {
res.json(status: true)
}
})
})
答案 0 :(得分:0)
您在哪里定义了类别?试试这个:
router.post('/',(req,res)=>{
var category = new Category() //assuming you defined newCategory as a call to the model
category.name = req.body.name;
category.img_url = req.body.img_url;
category.save((err,category)=>{ //here you're saving the
if (err) {
console.log(err);
}else {
res.json(status: true)
}
})
})