This thing works on server side fine for me, session successfully adds data in a cart as it should. But on client side (I'm using axios) the session is constantly being recreated. By recreating the data I mean for example when I try to add more items in cart, whole the time quantity is still 1 and it is being recreated constantly with last item.
This is file where I use session:
app.use(session({
secret: "hideSession",
resave: true,
saveUninitialized: true,
cookie: {
sameSite: true,
httpOnly: false,
}
}));
Whole add_to_cart
route request:
app.post("/add_to_cart/:name", async (req, res) => {
let db = await connect();
let cursor = await db.collection("products").find({})
let finalData = await cursor.toArray();
const name= req.params.name;
const singleDrink = await finalData.find((product) => product.name === name);
let cart;
if (!req.session.cart) req.session.cart = cart = new Cart({});
else cart = new Cart(req.session.cart);
req.session.cart = cart;
cart.addDrink(singleDrink);
console.log(req.session.cart,req.session.cookie)
res.send(cart);
});
client side using axios:
addToCart(name){
return Service.post(`/add_to_cart/${name}`)
}
This is expected output in console (property ukupnaKol
is quantity). I get that output on my server side:
This is output which I get (no matter how many items I add, property ukupnaKol
- quantity will always be 1):
This is the postman request (cookie/session settings also):
Via Active questions tagged javascript - Stack Overflow https://ift.tt/ZO6UirW
Comments
Post a Comment