Well, i have a conceptual problem and a real error... i'm working on an inventory module concept, is simple, it consist on two models:
- Inventory
- Item
The Inventory model just contains a "items" field, and that field is just a reference (to populete) to the real item object models, you know items: [type: String, ref: 'Item']
.
The Item object is the real data container, it have all data (name, itemCode, description, disponibility, existence... and so on). The problem in the concept is confusing for me.
When an item is created, it need to be pushed into the array "items" of Inventory model document, i coded this snippet, but it retrieve me an error:
var inventario = new Inventario(); //The inventory document instance
var inventarios = router.route('/inventarios');
inventarios.post(function(req, res) {
// itamdata object
var nuevoItem = {
_id: req.body._id,
descripcion: req.body.descripcion,
costo: req.body.costo,
precioMin: req.body.precioMin,
precioMax: req.body.precioMax,
existencia: req.body.existencia,
disponible:req.body.disponible
};
// Create a new item
Item.Create(nuevoItem, function(err, item) {
if(err) {
res.status(500).json({
msg: 'Problema interno con la base de datos',
error: err
});
}
// call push from push method of documents array
inventario.items.push({ _id: nuevoItem._id });
res.status(200).json({msg: 'Item Creado', token: item});
}); // fin Item.Create
}); //fin inventarios.post
The error is: /home/nano/Dev/JS/OMI/node_modules/express/lib/router/index.js:482
this.stack.push(layer);
^
TypeError: Cannot call method 'push' of undefined
My concept is really simple, and i tested the requires and exports of models, all looks fine, so, anyone have some idea to solution the problem?