node.js - 无法读取“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] 应用程序崩溃 - 在开始之前等待文件更改...
解决方案
我看到你的代码,在这一行你从模块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
推荐阅读
- angular - 将提供者添加到提供者数组不起作用
- opengl - 为什么没有适用于 Linux 的屏幕变形软件以及如何解决?
- python - 针对不同的问题给予不同的分数
- r - 为什么保存后无法使用 RDS 文件?
- java - Java: super() 不是在第一行可能吗?
- javascript - 闪电组件和 window.addEventListener()
- git - 无法将初始提交从远程仓库推送到新的 github 仓库
- docker - 具有不同容器操作系统和主机操作系统的 Docker
- javascript - 使用更新函数,将一个项目添加到数组中,但它会在其中不断更新
- spring-data-mongodb - 在聚合中使用忽略大小写进行排序