Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap localStorage returns the string "[object Object]" instead of the object

I trief to use the localStorage object in Phonegap, but instead of getting an object, the getItem only receives a string "[object Object]":

var storage = window.localStorage;
storage.setItem('test',{'name':'mark','greeting':'Hello'});
console.log(storage.getItem('test'));

In the console of Google chrome it says:

[object Object]

The output of "console.log(storage)" is the following:

Storage
...
test: "[object Object]"

If I try to access a property of the object it just says "undefined":

storage.getItem('test').name

Any ideas how to get this to work?

like image 937
hering Avatar asked Apr 01 '11 11:04

hering


People also ask

What does localStorage return?

localStorage is a read-only property that returns a reference to the local storage object used to store data that is only accessible to the origin that created it.

Does localStorage only store strings?

LocalStorage is a key/value datastore that's available on a user's browser. Like cookies, LocalStorage can only store string data for its keys and values.

What does localStorage getItem return?

The getItem() method returns value of the specified Storage Object item. The getItem() method belongs to the Storage Object, which can be either a localStorage object or a sessionStorage object.


1 Answers

HTML5 localStorageallows you to store strings only.

You'll have to perform a JSON.stringify when you store your object, and JSON.parse when you retrieve it.

like image 173
Martijn Avatar answered Oct 04 '22 12:10

Martijn