mongoose .push documentos incrustados en un modelo incrustado?
Frecuentes
Visto 288 equipos
0
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 });
});
});
});
1 Respuestas
1
To set the properties of bar
in the model, you'll have to create the additional Object
para ello:
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,
}
});
contestado el 23 de mayo de 14 a las 00:05
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas node.js mongodb mongoose or haz tu propia pregunta.