I know the syntax of it and how it works, but I cannot understand the internal workings, why does a method chaining require another method at one time, but doesn't some other time?
This code works fine
const cart = await Carts.findById(cartId).populate('product');
But this code does not
let cart = await Carts.findById(cartId);
cart = await cart.populate('product');
And to make it work, we use the execPopulate
method which works like this.
let cart = await Carts.findById(cartId);
cart = await cart.populate('product').execPopulate();
Now, as far as I have read method chaining in javascript, the code should run fine without the execPopulate
method too. But I cannot seem to understand why populate does not work on existing mongoose objects.