繁体   English   中英

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

[英]Pushing data into an array model in mongoose

好吧,我有一个概念上的问题和一个真正的错误……我正在研究库存模块的概念,很简单,它包含两个模型:

  • 库存
  • 项目

库存模型仅包含一个“ items”字段,并且该字段只是对实际项目对象模型的引用(以填充),您知道这些items: [type: String, ref: 'Item']

Item对象是真实的数据容器,它具有所有数据(名称,itemCode,描述,责任,存在等)。 这个概念中的问题令我感到困惑。

创建项目时,需要将其推入库存模型文档的数组“项目”中,我对该代码段进行了编码,但它向我检索了一个错误:

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

错误是: /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

我的概念很简单,我测试了模型的需求和导出,一切看起来都很好,所以,有人有解决问题的想法吗?

错误:

发送标头后无法设置。

当您在架构和api文件中或在HTML页面中发送字段名称错误或在req.body参数名称中发送错误时,将出现错误。
可以尝试使用另一种方法:

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

在您的server.js文件中。

暂无
暂无

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

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