so this is my app.js
the thing i want to do is when go the cart in my website, show me all the products in the cart with the user model that i created which has a field of cart in that there is cart items, which has two fields one product id, and quantity.
so i want to populate the product id so i can see their inf on and ofcourse quantity in my cart.
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const errorController = require('./controllers/error');
const User = require('./models/user');
const app = express();
app.set('view engine', 'ejs');
app.set('views', 'views');
const adminRoutes = require('./routes/admin');
const shopRoutes = require('./routes/shop');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use((req,res,next)=>{
User.findById('657167533934e81f519f9bbd')
.then(user=>{
req.user = user;
next();
})
.catch(err=>{
console.log(err)
})
})
app.use('/admin', adminRoutes);
app.use(shopRoutes);
app.use(errorController.get404);
mongoose.connect('mongodb+srv://feri:[email protected]/shop?retryWrites=true&w=majority')
.then(result=>{
User.findOne().then(user =>{
if(!user){
const user = new User({
name:'feri',
email:'[email protected]',
cart:{
items:[]
}
});
user.save();
}
});
app.listen(3000);
}).catch(err=>{
console.log(err);
})
and this is my shop controller. which i have a problem with the populate function.
exports.getCart = (req, res, next) => {
req.user
.populate('cart.items.productId')
.execPopulate()
.then(user => {
const products = user.cart.items;
console.log(products);
res.render('shop/cart', {
path: '/cart',
pageTitle: 'Your Cart',
products: products
})
})
.catch(err => console.log(err));
};
and this is my user model.
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema({
name:{
type: String,
required:true
},
email:{
type:String,
required: true
},
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();
}
module.exports = mongoose.model('User',userSchema);
and this is my product model.
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const productSchema = new Schema({
title: {
type:String,
required: true
},
price:{
type: Number,
required:true
},
description:{
type: String,
required: true
},
imageUrl:{
type:String,
required:true
},
userId:{
type:Schema.Types.ObjectId,
ref:'User',
required:true,
}
})
module.exports = mongoose.model('Product',productSchema);
i tried updating the mongoose.
changing the name an all that.
i dont know what is wrong anymore.your text

