簡體   English   中英

貓鼬與貓鼬; 在更新1個文檔時遇到麻煩

[英]mongodb with mongoose; trouble updating 1 document in

感謝您的關注。

這是我的代碼:

來自models / Cart.js

var mongoose = require('mongoose');

var CartSchema = mongoose.Schema({
    owner: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
    products: [ {type: mongoose.Schema.Types.ObjectId, ref: 'Product', quantity:     Number} ],
    created: Date,
    updated: Date
});


exports.Cart = mongoose.model('Cart', CartSchema);

來自cart.js

var model = require('../models/Cart');
//modify a cart
router.post('/update/:id', function(req, res, next){
  var cart = model.Cart();
  console.log(cart);
  cart.findByIdAndUpdate(req.params.id, {
    products: req.body.products,
    updated: moment().format()
  }, {new:true}, function(err, result){
    if(err) throw err;
    res.status(200).send(result);
  });
});

我得到的錯誤undefined is not a function在cart.findByIdAndUpdate上undefined is not a function ,也不是將購物車日志記錄為{ products: [], _id: 56b3b9fcd13a19a87bddd5f3 }

嘗試通過刪除或注釋以下行: //var cart = model.Cart(); 並將cart替換為cart.findByIdAndUpdate(......)行上的model

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM