如何对子属性发布请求

时间:2017-03-18 22:45:15

标签: javascript node.js mongodb mongoose mongoose-schema

如何在我的架构中向产品{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)
    }
  })
 })

1 个答案:

答案 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)
    }
  })
 })