0

I need to access to some props within an object, but I am getting an object as a string, this object comes from LocalStorage, what should I do to access to that props in that object ?

lobby: "[object Object]" there is the object

StillDead
  • 1,879
  • 2
  • 10
  • 12
  • How are you currently trying to access it? Some code would be nice. – Ivan Dec 07 '15 at 17:30
  • 1
    If you're just getting `"[object Object]"`, then the actual object was incorrectly serialized and it's gone forever. – Pointy Dec 07 '15 at 17:31
  • 1
    How is the object being *saved* to LocalStorage? If `"[object Object]"` is all that's being stored, you're out of luck. – Paul Roub Dec 07 '15 at 17:31
  • Try with `JSON.stringify(object);` if you need the object as string – michelem Dec 07 '15 at 17:31
  • here is how I am doing it `localStorage.setItem('apogeLiveLobbyData', tokenData.lobbyData);` the I getting it like this `lobby : localStorage.getItem('apogeLiveLobbyData'),` so, when I do `console.log(this.props)` , this comes up `lobby: "[object Object]"` @PaulRoub – StillDead Dec 07 '15 at 17:33
  • @Michelem no, the object comes like this `lobby: "[object Object]"` so I don't need a string, I need the object by itself – StillDead Dec 07 '15 at 17:34
  • 1
    You are setting the data wrong.... that is the problem. – epascarello Dec 07 '15 at 17:34

2 Answers2

3

localStorage stores strings. If you try to save an object, it will first call toString(), resulting in "[object Object]".

You're better off saving it as JSON:

localStorage.setItem( 'apogeLiveLobbyData', JSON.stringify(tokenData.lobbyData));

and retrieving it that way, too

lobby : JSON.parse( localStorage.getItem('apogeLiveLobbyData') )
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
2

You can not store an object into local storage since it holds strings. So you need to make it a string to store and than parse it to get it back to an object.

JSON.stringify()

localStorage.setItem('apogeLiveLobbyData', JSON.stringify(tokenData.lobbyData));  

JSON.parse()

var data = JSON.parse(localStorage.getItem('apogeLiveLobbyData'));
epascarello
  • 204,599
  • 20
  • 195
  • 236