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

Viewed 54

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

Related