Building an e-Commerce website with Node – Part 45 – Create Cart Route in Node.js

Now, we are going to create a Cart route. Open the main.js in the routes folder and type in the following route just above the line router.post('/product/:product_id' ….

router.get('/cart', function(req, res, next) {
  Cart
    .findOne({ owner: req.user._id })
    .populate('items.item')
    .exec(function(err, foundCart) {
      if (err) return next(err);
      res.render('main/cart', {
        foundCart: foundCart,
        message: req.flash('remove')
      });
    });
});

First we search in the database to see if that user._id exist.
Next we populate item.item.
Next we execute an anonymous function that renders the cart page and supply the page with data that can be used.

Leave a Reply