Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we add req.user object before using a method?

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);
    })
like image 226
KaranManral Avatar asked Apr 18 '26 02:04

KaranManral


1 Answers

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.)

like image 56
Quentin Avatar answered Apr 19 '26 15:04

Quentin