Home > Mobile >  Javascript object. JSON.stringify and local storage
Javascript object. JSON.stringify and local storage

Time:10-23

Why is cart object saved as "cart" in the local storage? while it should have been a object. Because of that, the cart isnt displayed as it is supposed to be.

This happened after i deployed the website in vercel. "cart" is string rather than an object,which is causing error.

The link of the site is : enter image description here

CodePudding user response:

In localStorage the value will be save as string even if it is an object. You need to parse it. Then you can access it as an object.

CodePudding user response:

When you set the item in localStorage you set the name as cart that's why it save as cart name. And localStorage set the value in string formate even if you set the object(JSON.stringify). if you need to use as object when you get the item then you need to parse it.

Example -

JSON.parse(localStorage.getItem('cart')
  • Related