【问题标题】:How to make post request to child attribute如何向子属性发出发布请求
【发布时间】:2017-03-19 09:15:25
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    您在哪里定义了类别?试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-08
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      相关资源
      最近更新 更多