Reputation: 883
Would like to .push fields of an embedded structure into an embedded model document on a .push method. http://mongoosejs.com/docs/2.7.x/docs/embedded-documents.html
The console errors this though: SyntaxError: Unexpected token .
..for pushing bar.this : req.body.this,
when concatenating for the embed in the embedded model
The models look like this:
var OneModel = new Schema({
foo: String,
bar: {
this : String,
that : Number,
}
});
var TwoModel = new Schema({
foo: String,
bar: {
this : String,
that : Number,
},
modelone: [OneModel]
});
And the NodeJS API looks this:
var ModelsOneTwo = require('./app/models/modelsonetwo');
router.route('/modeltwo/:modeltwo_id')
// update TwoModel with this _id
//(accessed at PUT http://localhost:4200/api/v1/modeltwo/:modeltwo_id)
.put(function(req, res) {
ModelsOneTwo.findById(req.params._id, function(err, modeltwo) {
if (err)
res.send(err);
// embedded document updating
// http://mongoosejs.com/docs/2.7.x/docs/embedded-documents.html
modeltwo.modelone.push({
foo : req.body.foo,
bar.this : req.body.this,
bar.that : req.body.that
});
// save the modeltwo, and check for errors
modeltwo.save(function(err) {
if (err)
res.send(err);
res.json({ message: req.params.modeltwo_id + ' ' + req.body.foo });
});
});
});
Upvotes: 0
Views: 304
Reputation: 123423
To set the properties of bar
in the model, you'll have to create the additional Object
for it:
modeltwo.modelone.push({
foo : req.body.foo,
bar : {
this : req.body.this,
that : req.body.that
}
});
This is similar to how it was defined in the schema:
var OneModel = new Schema({
foo: String,
bar: {
this : String,
that : Number,
}
});
Upvotes: 1