簡體   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