1

I am creating an app that has CRUD functionalities for localStorage. I am trying to save some data to localStorage via the method setItem(), but instead of successfully saving the data, I am getting the following error:

Error: Unexpected end of JSON input

Here is my Stackblitz URL: https://stackblitz.com/edit/github-efvm9d

To replicate the error:

  1. Click on 'Add new product' link
  2. Fill in the form and press the 'Add a product' button
  3. Observe how the console outputs the error message Error: Unexpected end of JSON input

I am expecting to be able to add data to localStorage via the setItem() method.

aleksejjj
  • 1,405
  • 2
  • 18
  • 28

1 Answers1

2

the line localStorage.setItem("rubberboots", rubberboots); in your code is not correct. rubberboots is an Object not a string. You should do:

localStorage.setItem("rubberboots", JSON.stringify(rubberboots));

BTW, clear your localStorage before trying again, the current value of rubberboots in your localstorage could be null now. It will throw an error when you call JSON.parse() function

Nguyen Phong Thien
  • 3,237
  • 1
  • 15
  • 36
  • Thanks, but now the function `addRubberboot()` only adds an empty array to the localStorage so that the end result is `[[]]`. Can you tell me what is the issue? – aleksejjj Feb 14 '19 at 15:51
  • ah, that is the different issue. I can help you to check it out tomorrow – Nguyen Phong Thien Feb 14 '19 at 20:54
  • 1
    Thanks for the offer, but I already got help in another question: https://stackoverflow.com/questions/54697492/an-empty-array-is-being-pushed-to-localstorage-instead-of-data/54697663#54697663 – aleksejjj Feb 14 '19 at 21:58