Array[0] but still data in it - javascript

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"]

Related

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

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.

JavaScript Trying to read object giving me undefined on console

I am trying to read some data what I have.
My console is showing that it looks like this:
{
"calls":"0",
"ids":[
{
"id":"5117-9e39-7afd-46b3-a066-14e4-3433-871f",
"feat":{
"dates":{
"exp":"2018-10-02"
},
"read":[
{
"name":"Joe"
}
]
}
}
]
}
So I'm trying to console.log so it gives me the name.
I've done this:
console.info(data.ids.read.name)
This is returning undefined.
How can I get it to return the name?
Based on your comment showing the whole of data, you would need to use data.ids[0].feat.read[0].name to get the name.
Note that ids and read hold arrays, hence the need for the [0].
based on what you describe I think your problem is caused by you logging an object and logging a particular member of the object.
The console.log statement will give you a preview that you can expand, the preview shows the object as it is when you log it. It will sometimes give you an option to expand it and that expanded view will show the object as it is now.
When you log a primitive value or get a non expandable preview then you'll always see the object as it is when you log it.
Nikhil's suggestion to log the JSON string from the object is a good tip since you will log the object as is when you log it (string is primitive and won't give you an expand option). You could also use console.table.
Below you can see the behavior and difference of preview and expanded (you can run it in the browser's console):
var arr = [];
console.log('empty array?',arr);//previews to [] but expands to array with one object
console.log('undefined wont change',arr[0])//logs undefined because arr is still empty
arr.push({name:'ben'});//mutate array to add one item
You may be logging something that is mutated later by a resolving promise or from a callback.

Value is null even if it's not in console.log (...)

I'm using Angular and I'm trying to access property img of my object, and when I'm doing console.log(..) of my whole object. Value is there, but when I'm doing console.log(..) of object's property it said it's null.
console.log("Product:", this.article);
var url = this.article.img;
console.log("Image" , url);
Console:
I don't know how is this possible if this.article.img; looks like has some value in console.log. But when I try to access it, it says it's null. How come?
The object isn't rendered immediately. Your img property has a null value at logging time but when you open the object in the console, later, it's filled.
You can check that by logging console.log(JSON.stringify(this.article)).
The most probable reason of your problem is some asynchronous code whose achievement you're not correctly waiting for. As the object is taken from a database as you said, I guess you forget to use the object in the callback (which might be promise based).
Instead of logging:
console.log("Product:", this.article);
var url = this.article.img;
console.log("Image" , url);
try debugging, so that you can inspect the value in real time:
console.log("Product:", this.article);
debugger;
...
Because the browser console works with references in most modern clients:
Please be warned that if you log objects in the latest versions of Chrome and Firefox what you get logged on the console is a reference to the object, which is not necessarily the 'value' of the object at the moment in time you call console.log(), but it is the value of the object at the moment you click it open.
See: https://developer.mozilla.org/en-US/docs/Web/API/Console/log

Can not access and change property of a variable

I am trying to modify a property of an object.
console.log(dataset);
// -> This shows object with an 'ids' property, and it is an array with 7 elements.
But
console.log(dataset.ids); //shows an empty array
Please take a look at this image, it describles how 'dataset' look like: weired js
Can you explain why?
Thank you!
It's probably because your dataset.ids array isn't filled yet on the init.
Why?
When your open your panel from the Chrome console to inspect in details of dataset, Chrome console just recall a console log on the reference object.
At this time, the array is filled.
You should probably search a hook from your KanbanView object where you are sure this array is filled if you want to access it.
This console behavior could be confusing the first time when you face such case!

Unable to get properties of JavaScript Object

I'm returning JSON from a php file to a jQuery function, and am getting an object output with the properties agt_id and agt_type. With the webkit dev tools I can print see the object, expand it and see the properties. However, I get undefined when I try:
console.log(output.agt_id);
Here's what I'm seeing in my console with console.log(output):
From your screen shot, it looks like your object is in an Array. You can access the object at index 0 of the Array.
console.log(output[0].agt_id);

Categories