2017-03-18 3 views
0

Wie ich eine Post-Anforderung an das Produkt {type: mongoose.Schema.Types.ObjectId, ref: 'Product'} in meinem Schema machen tue, ist das, was mein Schema wieWie Post-Anforderung zu Kind Attribut machen

const CategorySchema = mongoose.Schema({ 
name: { 
type: String, 
required: true 
}, 
img_url:{ 
type:String, 
required: true 
}, 
product: [ 
{type: mongoose.Schema.Types.ObjectId, ref: 'Product'} 
] 
}) 

aber ich, wie ich schreiben kann nicht wissen, schaut zu das Produkt-Array?

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

Antwort

0

Wo haben Sie die Kategorie definiert? Versuchen Sie Folgendes:

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