Question

I have an object pulled from a form on my page. I push this object into an array.

var postObj = {title:$('input').val(),body:$('textarea').val()}
postArray.push(postObj);
localstorage.setItem("posts", JSON.stringify(postArray));

However, I'm just getting Uncaught ReferenceError: Localstorage is not defined.

In the console I'm getting these responses from testing.

postObj
Object {title: "title", body: "dog"}
postArray
[
Object
body: "dog"
title: "title"
__proto__: Object
]
JSON.stringify(postArray);
"[{"title":"title","body":"dog"}]"

Most responses I've found to similar questions are saying to use JSON.stringify(array). I don't know why when I use JSON.stringify as the data parameter of .setItem it is not working.

Était-ce utile?

La solution

Looks like you have a typo, should be:

window.localStorage // note the camelCase name

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top