I am understanding some basic CRUD operations on MongoDB, I am having difficulty in understanding why we use req.user before using a method inside the promise below -
Why can't we use return addToProduct() instead of req.user.AddToProduct()
exports.postCart = (req, res, next) => {
const prodId = req.body.productId;
Product.findById(prodId)
.then(product => {
return req.user.addToCart(product);
})
.then(result => {
console.log(result);
})
Because addToCart is a method of the user object and not a variable in scope for the current module.
(And speculating, you are probably adding to the cart of a specific user so you need to tell the method which user's cart to add to.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With