问题描述
我正在从事购物车项目,我想将产品添加到购物车中,例如,当用户登录并将随机产品添加到购物车中时,该产品会与ID一起添加 所以这是我的购物车获取并发布代码
exports.getCart = (req,res,next) => {
req.user
.populate('cart.items.productId')
.execPopulate()
.then(user => {
console.log(user)
const products = user.cart.items;
res.render('shop/cart',{
path: '/cart',pageTitle: 'Your Cart',products: products
});
})
.catch(err => console.log(err));
};
exports.postCart = (req,next) => {
const prodId = req.body.productId;
Product.findById(prodId)
.then(product => {
return req.user.addToCart(product);
})
.then(result => {
console.log(result);
res.redirect('/cart')
});
};
当我单击购物车中的按钮时,该代码有效,并且随着用户和productid一起添加到购物车的产品数量增加,我可以看到数据库中的更改,但随后出现错误说
TypeError: D:\sessions\views\shop\cart.ejs:12
10| <% products.forEach(p => { %>
11| <li class="cart__item">
>> 12| <h1><%= p.productId.title %></h1>
13| <h2>Quantity: <%= p.quantity %></h2>
14| <form action="/cart-delete-item" method="POST">
15| <input type="hidden" value="<%= p._id %>" name="productId">
Cannot read property 'title' of null
这是我的购物车视图ejs代码
<main>
<% if (products.length > 0) { %>
<ul class="cart__item-list">
<% products.forEach(p => { %>
<li class="cart__item">
<h1><%= p.productId.title %></h1>
<h2>Quantity: <%= p.quantity %></h2>
<form action="/cart-delete-item" method="POST">
<input type="hidden" value="<%= p._id %>" name="productId">
<button class="btn danger" type="submit">Delete</button>
</form>
</li>
<% }) %>
</ul>
<hr>
<div class="centered">
<form action="/create-order" method="POST">
<button type="submit" class="btn">Order Now!</button>
</form>
</div>
<% } else { %>
<h1>No Products in Cart!</h1>
<% } %>
</main>
这是我的app.js
app.use((req,next) => {
User.findById('5f79f8f231e4f319009071de')
.then(user => {
console.log(user)
req.user = user;
next();
})
.catch(err => console.log(err));
});
mongoose
.connect(
'mongodb+srv://<username>:<password>@cluster0.lltqs.mongodb.net/shop?retryWrites=true&w=majority'
)
.then(result => {
User.findOne().then(user => {
if (!user) {
const user = new User({
name: 'Max',email: 'max@test.com',cart: {
items: []
}
});
user.save();
}
});
app.listen(3000);
})
.catch(err => {
console.log(err);
});
任何人都可以告诉我我做错了什么事吗,我真的无法弄清楚,稍加指导会很有帮助 编辑:这是我的猫鼬模型和addToCart函数的外观 addtocart函数有效,但是唯一的问题是应用程序抛出错误
const userSchema = new Schema({
name: {
type: String,required: true
},email: {
type: String,cart: {
items: [
{
productId: {
type: Schema.Types.ObjectId,ref: 'Product',required: true
},quantity: { type: Number,required: true }
}
]
}
});
userSchema.methods.addToCart = function(product) {
const cartProductIndex = this.cart.items.findIndex(cp => {
return cp.productId.toString() === product._id.toString();
});
let newQuantity = 1;
const updatedCartItems = [...this.cart.items];
if (cartProductIndex >= 0) {
newQuantity = this.cart.items[cartProductIndex].quantity + 1;
updatedCartItems[cartProductIndex].quantity = newQuantity;
} else {
updatedCartItems.push({
productId: product._id,quantity: newQuantity
});
}
const updatedCart = {
items: updatedCartItems
};
this.cart = updatedCart;
return this.save();
};
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)