-1

I'm saving data in local storage, but having a hard time parsing/not parsing in the appropriate places to use the object properties when I need them, or stringify them when they need to be saved.

When the console logs the following (and I'm in chrome, so the key is purple and the value is red):

Object {foo: bar}

I'm able to access the properties correctly.

When the console logs

[object Object]

I can't access the properties. If this means it's a string, why can't I parse it? If it's already an object, why can't I access its properties?

Edit

The question I should have asked is: how do I convert this string to an object with which I can access properties etc?

asparism
  • 604
  • 1
  • 8
  • 17
  • That means it's a string, yes. You can’t parse it because the `ToString` of an object isn’t a serialization of the object (not all objects are serializable). – Ry- May 08 '17 at 18:39
  • 1
    Possible duplicate of [what does \[object Object\] mean?](http://stackoverflow.com/questions/4750225/what-does-object-object-mean) – Sebastian Simon May 08 '17 at 18:39
  • 1
    What do you mean "parse it"? How do you parse the string "[object Object]" into anything meaningful? For localStorage, `JSON.stringify()` when setting, `JSON.parse()` when getting. – Heretic Monkey May 08 '17 at 18:39
  • One is an object, the other used to be but isn't an object anymore. – Kevin B May 08 '17 at 18:44
  • possible duplicate? X/Y problem? http://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage – Kevin B May 08 '17 at 18:51
  • I am using `JSON.stringify()` to set and `JSON.parse()` to get, yes. What is serialization? – asparism May 08 '17 at 18:52
  • the conversion from one format to another – Kevin B May 08 '17 at 18:52
  • If you're using JSON.stringify() and JSON.parse(), how are you getting [object Object]? neither create that output. – Kevin B May 08 '17 at 18:53
  • Kevin B's possible duplicate was close, but the problem I'm having is using `JSON.stringify()` to save in localStorage and `JSON.parse()` when I get it, [link]http://stackoverflow.com/questions/43661936/string-set-in-localstorage-gets-as-object – asparism May 08 '17 at 18:55
  • Per your edit... that's impossible. You instead must back track until you have an object to work with. Once it's in that string form, there's no going back. – Kevin B May 08 '17 at 19:00
  • @KevinB Have you looked at the link in my previous comment? Concerning storage, isn't the idea to stringify an object to place it in local storage, and then parse it back when you get it? Which strings can be parsed and which can't? Do I need to stringify and save the object differently? – asparism May 08 '17 at 19:04
  • I dunno, you haven't shown your code. per the question i linked, clearly using stringify and parse properly works. you must be doing something improperly. – Kevin B May 08 '17 at 19:04
  • per your link... i think there's more going on there than the OP presented. – Kevin B May 08 '17 at 19:07
  • here is the [jsfiddle](https://jsfiddle.net/asparism/Lr0foyLt/). you can replicate the problem by commenting and uncommenting the localStorage.clear() in the beginning. – asparism May 08 '17 at 19:08
  • per your fiddle... `localStorage.setItem("_asparism_"+this.props.name, theDish);` wut, theDish is... not something you should be putting in local storage. (toward the end) – Kevin B May 08 '17 at 19:21
  • @KevinB need to stringify before setting? – asparism May 08 '17 at 19:25
  • well, it's a component... not the kind of thing you'd put in localstorage. I don't quite understand what you were trying to do with that line. – Kevin B May 08 '17 at 19:28

1 Answers1

-1

[object Object] is a string representation of the object, similar to calling object.toString()

To get a detailed log, use dir() (exclusive to Google Chrome Dev Tools)

enter image description here

  • To stringify an object, use JSON.stringify(object).

  • To restore an object back from string, use JSON.parse(string).

user7401478
  • 1,372
  • 1
  • 8
  • 25
  • i found the answer to be not useful (as indicated by the downovote.) – Kevin B May 08 '17 at 18:47
  • or at best unclear. yes, json.stringify/parse should probably be used in the OP's scenario, but why? – Kevin B May 08 '17 at 18:49
  • Do I use console.dir(theObject) ? How would I then access the properties or convert it to an object? – asparism May 08 '17 at 18:57
  • you wouldn't, that's just a debugging tool. [object Object] is just a useless string, you can't gain anything out of it other than the fact that it *was* an Object until it was converted to a string. – Kevin B May 08 '17 at 18:57
  • @asparism you should access values just fine if that's an object. Use `dir()` to debug the case when you are not getting what you want. – user7401478 May 08 '17 at 18:59
  • @WearyAdventurer console.dir(theObject) returns the same string. It doesn't seem to be an object, it seems to be a string which for some reason can't be parsed into an object. – asparism May 08 '17 at 19:01
  • @asparism Try serializing the object with `JSON.stringify()` and see what it throws out. Where are you getting this object, what do you assign to it? – user7401478 May 08 '17 at 19:04
  • @WearyAdventurer `JSON.stringify()` logs "[object Object]". the problem is replicated in this [link](https://jsfiddle.net/asparism/Lr0foyLt/). You can comment and uncomment the localStorage.clear() at the beginning to see the issue. – asparism May 08 '17 at 19:07
  • @asparism There might be an issue with react. Do you absolutely need to use it? – user7401478 May 08 '17 at 19:11
  • After removing all of the extra junk, it works fine. – Kevin B May 08 '17 at 19:12
  • @WearyAdventurer I'd like to keep it, yes. – asparism May 08 '17 at 19:13
  • @KevinB By extra junk do you mean everything except the storage code? – asparism May 08 '17 at 19:13
  • @asparism yes. And to test it i commented out all the var a b c etc leaving just the .key(0) and .getItem(key). – Kevin B May 08 '17 at 19:14
  • @KevinB do you have a forked fiddle by chance? and thanks, i'll try to clean it up and see if it starts working. – asparism May 08 '17 at 19:18