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.

1
  • Please explain why the downvote? I could improve the question. Commented Feb 14, 2019 at 15:12

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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?
ah, that is the different issue. I can help you to check it out tomorrow
Thanks for the offer, but I already got help in another question: stackoverflow.com/questions/54697492/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.