Actually, I’ve been thinking it has to do with the redirect after adding to cart, not the DOM manipulation.
This is the index route that contains the search-
router.get('/', isLoggedIn, function (req, res, next) {
var noMatch = null;
var successMsg = req.flash('success')[0];
if(req.query.search) {
var regex = new RegExp(escapeRegex(req.query.search), 'gi');
Product.find({description: regex}, function(err, products) {
if(products.length === 0) {
noMatch = 'Ese producto no se encuentra. Pruebe con otra palabra, o haga click en "Falta Producto!"';
}
res.render("shop/index", {title: 'eStationery', products: products, successMsg: successMsg, noMessages: !successMsg, user: req.user, noMatch: noMatch});
});
} else {
Product.find(function(err, products) {
res.render("shop/index", {title: 'eStationery', products: products, successMsg: successMsg, noMessages: !successMsg, user: req.user, noMatch: noMatch});
});
}
});
So, basically first would search for the query, or show all products by defaullt.
But, everytime I click on an item to be added to the cart, it redirects to '/'
-
router.get('/add-to-cart/:id', isLoggedIn, function(req, res, next) {
var productId = req.params.id;
var cart = new Cart(req.session.cart ? req.session.cart : {});
Product.findById(productId, function(err, product) {
if (err) {
return res.redirect('/');
}
Product.findByIdAndUpdate(req.params.id, {$set: {tempStock: product.tempStock + 1, stock: product.stock -1}}, function(err, updatedProduct) {
if (err) {
return res.redirect('/');
}
});
cart.add(product, product.id);
req.flash('success', 'Producto agregado al carrito.');
req.session.cart = cart;
res.redirect("/");
});
});
So, I’m thinking I should tell the app to remain on the current query if there is one, no? something like 'res.redirect(/?search=value)'
?
Am I close?
Edit and solution: no, I was not.
I just had to change the redirect from "/"
to "back"
, so after sending the item -which goes as /add-to-cart/:id
added to the cart, it would just return to the same page.