首页 > 解决方案 > 无法读取“ID 参数”的属性

问题描述

这是我的路由器代码,我在其中导入了我的 cart.js 模型

var router = require('express').Router();
var Product = require('../models/products');
var Cart = require('../models/cart'); 

router.get('/add-to-cart/:_id',(req,res,next)=>{
  const productID = req.params._id;
  var cart = new Cart(req.session.cart ? req.session.cart : {});

  Product.findById(productID,(err,product)=>{
    if(err){
      res.redirect('/');
    }
    cart.add(product, product.id);
    req.session.cart = cart;
    res.redirect('/');
  })  
})

这是我的 Cart.js 代码

module.exports = function cart(oldCart){
   this.items = oldCart.items;
   this.totalQty = oldCart.totalQty;
   this.totalPrice = oldCart.totalPrice;

   this.add = function(item,id){
      var storedItem = this.items[id]; // Here is Problem
      if(!storedItem){
        storedItem = this.items[id] = {item: item, qty: 0, price: 0};
      }
      storedItem.qty++;
      storedItem.price = storedItem.item.price * storedItem.qty;
      this.totalQty++;
      this.totalPrice = storedItem.price;
   };

   this.generateArray = function(){
      var array = [];
      for (var id in this.items){
        array.push(this.items[id]);
      }
      return array;
   };
}

错误:

events.js:174 抛出错误;// 未处理的“错误”事件 ^

类型错误:无法读取未定义的属性“5d9182475f539435e81a7bb9” 在 Cart.add (C:\Users\Manav\Documents\Github\E_com_App\models\cart.js:7:36) 在 Product.findById (C:\Users\Manav\Documents\Github\E_com_App\routes\routes. js:85:10) 在 C:\Users\Manav\Documents\Github\E_com_App\node_modules\mongoose\lib\model.js:4589:16 在 C:\Users\Manav\Documents\Github\E_com_App\node_modules\mongoose \lib\query.js:4323:12 在 process.nextTick (C:\Users\Manav\Documents\Github\E_com_App\node_modules\mongoose\lib\query.js:2805:28) 在 process._tickCallback (internal/process /next_tick.js:61:11) 在 C:\Users\Manav\Documents\Github\E_com_App\node_modules\mongoose\lib\model.js:4591:13 在 C:\Users\Manav 处发出“错误”事件\Documents\Github\E_com_App\node_modules\mongoose\lib\query.js:4323:12 at process.nextTick (C:\Users\Manav\Documents\Github\E_com_App\node_modules\mongoose\lib\query.js:2805: 28) 在过程中。_tickCallback (internal/process/next_tick.js:61:11) [nodemon] 应用程序崩溃 - 在开始之前等待文件更改...

请参阅我的完整代码:https ://github.com/ma-9/E_com_NodeJS.git

标签: node.jsmongodbexpressmongoosebody-parser

解决方案


我看到你的代码,在这一行你从模块Product.findById(productID,(err,product)=>{调用findById函数。product但我只在这个文件中看到

const mongoose = require('mongoose');
const schema = mongoose.Schema;

const productSchema = new schema({
    imagePath: {type: mongoose.Schema.Types.String, required:true},
    // imagePath2: {type: mongoose.Schema.Types.String, required:true},
    // imagePath3: {type: mongoose.Schema.Types.String, required:true},
    // imagePath4: {type: mongoose.Schema.Types.String, required:true},
    // imagePath5: {type: mongoose.Schema.Types.String, required:true},
    name: {type: mongoose.Schema.Types.String, required:true},
    desc: {type: mongoose.Schema.Types.String, required:true},
    price: {type: mongoose.Schema.Types.Number, required:true},
});

module.exports = mongoose.model("DND - Products",productSchema);

您必须在本节中定义所需的功能。例如:findById


推荐阅读