mern-stack e commerce- get all user cartItem from database before cartUpdate to merge local storage cart item with database cart item.

0
Posted at June 4, 2021 27 Views . 0 Saved .

hello sir,

I want to get all the cart Items of current user from database while executing updateCart function but i can only get localstorage cart item. is it possible to run updateCart function only after i get all cart-item of user from database.

scenario:

1.user add some items to cart -which is initially saved in localstorage and with update cart we get it in state aswell even after page reload.

2. now when user log in to his account he already have some of the item saved in database. so, when the item (before login item) matched with (loged in user item) i want to sync the quantity of those item and update cart.

4 Comments
Rizwan Khan
q: now when user log in to his account he already have some of the item saved in database. so, when the item (before login item) matched with (loged in user item) i want to sync the quantity of those item and update cart. Sync is also part of updateCart function
Rizwan Khan
q: user add some items to cart -which is initially saved in localstorage and with update cart we get it in state aswell even after page reload. Yes it is like exactly like this now
Rizwan Khan
q: I want to get all the cart Items of current user from database while executing updateCart function but i can only get localstorage cart item. I guess updateCart function is to update cart items on UI level as i dont remember exactly so yes if you are not logged in everything will be in localStorage after your login localStorage item will be pushed to database. Now next time when you will visit page and add items without login it will be in localstorage after login it will be merged with existing cart as per current flipkart clone implementation. q: is it possible to run updateCart function only after i get all cart-item of user from database. As per the current implementation after your login cart items will be fetch from database first then updateCart function will be update on UI
Pabin Limbu
thank you sir for the reply. with every bug - fixing you taught us the ways to think different solutions. learned many things from you and i am still in mid lesson hoping to learn more ..
Pabin Limbu
how do i get the cart from database first and update cart with local storage card .. please help
Rizwan Khan
localStorage works as temporary storage before login once you are logged in then your localStorage item will be update in the database. this is how it is implemented in flipkart clone