The add function need create subdoc under the Person for many Exercise and I get undefined and manual in mongoose is poor. Please help
var Schema = mongoose.Schema;
var childSchema = new Schema({
// Single nested subdocuments. Caveat: single nested subdocs only work
// in mongoose >= 4.2.0
description:String,
duration:Number,
date:{ type: Date, default: new Date() }
});
var blogSchema = new Schema(
{ name: {
type: String,
required: true},
child: childSchema
});
and route
app.post("/api/exercise/add", function (req, res ) {
console.log(req.body)
var personId = req.body.userId
Person.findById(personId,
function (err, docs) {
if(!docs) { res.send('First add new user')}
else{
docs.save(function(err, docs) {
if (err) res.json({err:err});
console.log(docs)
**Problematic how to ?**
docs.child.create({description:req.body.description,duration:req.body.duration,date: req.body.date})
res.json(docs);
});
}
})
})
The output from logs looks
{ userId: ‘5b56476f0a78de4d3ed85775’,
description: ‘vvcvbhj’,
duration: ‘2’,
date: ‘08.05.2018’ }
{ _id: 5b56476f0a78de4d3ed85775, name: ‘lubodrinka’, __v: 0 }
The mongoose’s manual is crazy, but I found the second way with array declaration child: [childSchema] work and instead docs.child.create the docs.child.push