0

Is there something wrong with the way I'm initializing my object and Array. I'm trying to save Cart data into the localStorage. I'm getting the data from the state, and it displays when I console. logged in, and the object seems to work but not the array, I don't understand what is wrong with the code.

Unhandled Rejection (TypeError): cartItemObjectArray.push is not a function

edit: however it seems that the if statement thinks that the local storage is not undefined even when it is, and I don't know how to resolve that too.

below is inside my constructor

constructor(props) {
    super(props);
    this.state = {
      comicId: null,
      selectedOption: null,
      comicPrice: null,
      purchaseType: null,
      priceCurrency: "$",
      currencyVisibility: null,
      issueObjectState: null,
      seriesObjectState: null,
      pricingObjectState: null,
    };
  }

Below is my onSubmit() so that whenever te user hits add to cart the state values will be retrieved and set

onSubmit = async (event) => {
    //submit function when adding item to cart

    if (localStorage.getItem("cartitem") != undefined) {
      //if there are items in the array then do this
      console.log("It came not undefined");
      let cartItemObjectArray = localStorage.getItem("cartitem");
      let cartItemObject = {
        comicid: this.state.comicId,
        purchasetype: this.state.purchaseType,
      };
      console.log(cartItemObject);
      cartItemObjectArray.push(cartItemObject);
      localStorage.setItem("cartitem", cartItemObjectArray);
      toast.success("Item Added to cart");
      console.log("This is the mighty array ", cartItemObjectArray);
    } else {
      //if there are no items in the cart array in the local storage then initialize and do this
      console.log("It came undefined");
      let cartItemObjectArray = [];
      let cartItemObject = {
        comicid: this.state.comicId,
        purchasetype: this.state.purchaseType,
      };
      console.log(cartItemObject);
      cartItemObjectArray.push(cartItemObject);
      localStorage.setItem("cartitem", cartItemObjectArray);
      toast.success("Item Added to cart");
      console.log("This is the mighty array ", cartItemObjectArray);
    }

else if (cartItemFromLocalStorage == null) {
      //if there are no items in the cart array in the local storage then initialize and do this
      console.log("It came undefined");
      let cartItemObjectArray = [];
      let cartItemObject = {
        comicid: this.state.comicId,
        purchasetype: this.state.purchaseType,
      };
      console.log(cartItemObject);
      cartItemObjectArray.push(cartItemObject);
      localStorage.setItem("cartitem", JSON.stringify(cartItemObjectArray));
      toast.success("Item Added to cart");
      console.log("This is the mighty array ", cartItemObjectArray);
    }
  };
1
  • cartItem in localStorage is a string. So you need to deserialize it with JSON.parse first before adding items to the array
    – codemax
    Commented Sep 8, 2020 at 0:12

2 Answers 2

1

local storage only supports strings. Try stringifying the array to save it, and then parsing it when getting it from local storage.

// Save the array in local storage
localStorage.setItem("cartitem",JSON.stringify(cartItemObjectArray));


// Retrieve the array from local storage
var cartItemObjectArray = JSON.parse(localStorage.getItem("cartitem"));
4
  • nope it didnt work, I had created the array in another position in the if else. Commented Sep 8, 2020 at 2:11
  • when I printed the JSON.parse() through array the console.log is showing as "[object Object]" Commented Sep 8, 2020 at 2:12
  • 1
    Can you provide the error you got? Or add the updated code to questions is that we can see how you implemented the solution provided Commented Sep 8, 2020 at 2:13
  • update the post with the code using the JSON.stringify and the JSON.parse properly so we can see your code again
    – MatiasG
    Commented Sep 8, 2020 at 3:19
0

Because localStorage just store the data with construction key-value so you need to convert the array to Javascript String Object before store it. You have change all setItem in your code:

localStorage.setItem("data",JSON.stringify(cartItemObjectArray));

Remember to retrive the Javascript String Object before you can show data on UI:

JSON.parse(localStorage.getItem("data"))
1
  • 1
    JSON.stringify(localStorage.getItem("data")) should be JSON.parse(localStorage.getItem("data"))
    – lpradhap
    Commented Sep 8, 2020 at 2:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.