Can't access object properties even though I can see they exist - javascript

I can't access any of the object properties, keep getting undefined.
I have tried
console.log(JSON.parse(this.$store.state.user.userId));
and
console.log(JSON.parse(this.$store.state.user[0].userId));
When I do
console.log(JSON.parse(this.$store.state.user.userId));
I get
"SyntaxError: Unexpected token u in JSON at position 0"
When I just do
console.log(JSON.parse(this.$store.state.user));
I get the object and I can see the properties. It's just that whenever I try to access them I get undefined.

When I just do console.log(JSON.parse(this.$store.state.user)); I get the object and I can see the properties.
This means that this.$store.state.user contains JSON string which describes the user object.
Thus JSON.parse(this.$store.state.user.userId) is incorrect. In this case you are trying to get property userId from string, getting undefined and JSON.parse function fails on the first symbol, which is 'u'.
You should use JSON.parse(this.$store.state.user).userId instead.

Related

What object has a String representation of `{name=value}` in JavaScript?

I receive a complex value in JavaScript (inside the JDK Nashorn engine), that I have to interact with. That value prints to the console as {shown=true}. When I say typeof value I receive object as an answer. When I say Object.keys(value); I receive a TypeError: {shown=true} is not an Object in .... Whey I say value.shown or value["shown"] I always receive a null.
What is type is this mysterious object, and how do I access the value of the "shown" property correctly?
Unfortunately, it is not easy to create a simple example and I cannot debug interactively... Any help is highly appreciated!
Edit:
JDK is JavaSE-1.8.
Calling JSON.parse(value); results in
javax.script.ScriptException: SyntaxError: Invalid JSON: <json>:1:1 Expected , or } but found s
{shown=true}
^
Assuming what you got is a Java object, you should be able to call value.getClass() to get its Java class.
From its string representation, it might be an instance of java.util.HashMap or similar. If so, you should be able to access the value of the "shown" property via value.get("shown").

Returns undefined on a object prop even though it exists-JS

I'm having the same issue as mentioned here: Can't access object property, even though it exists. Returns undefined
However in my case it's a property with data.hostId.id, where data comes in as full object and hostId is shown undefined when its actually present in the json object, here's how it looks:
As you can see, I'm trying to access the qgUSerHost(at the very end) from the object and it throws undefined. I tried setTimeout, not working…
I also tried the solution mentioned in that post but nothing helped.
Any ideas on this one?
I think you should access that property like this.hostAsset["qgUserHost.id"] because of the . in the property name, so it wouldn't try to access the property id from some qgUserHost object.
'this.hostAsset.qgUserHost.id' != 'this.hostAsset.qgUserHost'

Cannot read property 'MyProperty' of undefined

In my Angular2 template, I have the following binding:
{{Stringify(result)}}
{{result.MyProperty}}
Stringify is a function that returns JSON.stringify of the input object. The Stringify function returned a JSON string that shows the name and value of MyProperty.
However, the second line returns a
TypeError. Cannot read property 'MyProperty' of undefined in {{result.MyProperty}}.
JSON.stringify clearly shows that this property / field exists, so why am I getting an error?
If it's there it can be accessed and JS wouldn't throw
Try instead
{{result?.MyProperty}}
maybe Angular makes an attempt to access result.MyProperty before result has a value while Stringify(result) doesn't choke on null.
When result is updated in the meantime (maybe because the value was received from the server, the view would update before you can recognized that an empty string was shown before.
Your question doesn't provide enough context to know.
See also
https://github.com/angular/angular/issues/791
https://angular.io/docs/ts/latest/guide/template-syntax.html#!#expression-operators (from a comment below - thanks to #MarkRajcok )

Javascript Objects and keys

The following command shows the properties of an Object.
var keys = Object.keys(result);
Output: [requester.client.id,request.id]
When I try to print an alert(result[request.id]) or alert(result.request.id) I dont get the values. Is there something I am missing?
In JavaScript objects keys are strings, though they can have periods. What you probably getting as the output is ['requester.client.id','request.id'], so it should be accessed as result['requester.client.id'].
Your result object has properties named "requester.client.id" and "request.id".
You need to do alert(result["request.id"]).
result[request.id] does not work because request here is treated as a variable name, and you probably have no variable named request.
result.request.id is closer, but it also fails because the property name has a period in it, so the parser treats this as the the id property of the request property of result.

Array[0] but still data in it

I'm debuging my code with Chrome DevTools and I have a strange ocasion.
I have an array which which contains data but console shows Array[0]
When I try to access it, it gives me an arror SyntaxError: Unexpected token ILLEGAL
Let me show you the details:
There is an Object I call from console "quick_chat", when I expand this object there is another Object called data, which shows like data: Array[0], but I'm still able to expand it where there is another object with the Id of my chat.
as you see here data is Array[0] but with data in it.
How can I access it with console? any suggestions?
That data is stored in a property of the array, and would be accessed using:
quick_chat.data["3SWA8h0clcAi"]

Categories