简体   繁体   English

用猫鼬将数据推入数组模型

[英]Pushing data into an array model in mongoose

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'] . 库存模型仅包含一个“ items”字段,并且该字段只是对实际项目对象模型的引用(以填充),您知道这些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). Item对象是真实的数据容器,它具有所有数据(名称,itemCode,描述,责任,存在等)。 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 错误是: /home/nano/Dev/JS/OMI/node_modules/express/lib/router/index.js:482 this.stack.push(layer); ^ TypeError: Cannot call method 'push' of undefined : /home/nano/Dev/JS/OMI/node_modules/express/lib/router/index.js:482 this.stack.push(layer); ^ TypeError: Cannot call method 'push' of undefined /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? 我的概念很简单,我测试了模型的需求和导出,一切看起来都很好,所以,有人有解决问题的想法吗?

Error: 错误:

Can't set headers after they are sent. 发送标头后无法设置。

Error comes when you are sending the name of the field in schema and in api file wrong or in your HTML page and req.body parameter names incorrect. 当您在架构和api文件中或在HTML页面中发送字段名称错误或在req.body参数名称中发送错误时,将出现错误。
Another way can be try using: 可以尝试使用另一种方法:

app.use(bodyParser.json({limit: '5mb'})); 

in your server.js file. 在您的server.js文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM