Below, you can see the output from these two logs. The first clearly shows the full object with the property I\'m trying to access, but on the very next line of code, I can\
For me it turned out to be a Mongoose-related problem.
I was looping over objects that I got from a Mongo query. I just had to remove:
items = await Model.find()
And replace it by:
items = await Model.find().lean()
Check if inside the object there's an array of objects. I had a similar issue with a JSON:
"terms": {
"category": [
{
"ID": 4,
"name": "Cirugia",
"slug": "cirugia",
"description": "",
"taxonomy": "category",
"parent": null,
"count": 68,
"link": "http://distritocuatro.mx/enarm/category/cirugia/"
}
]
}
I tried to access the 'name' key from 'category' and I got the undefined error, because I was using:
var_name = obj_array.terms.category.name
Then I realised it has got square brackets, that means that it has an array of objects inside the category key, because it can have more than one category object. So, in order to get the 'name' key I used this:
var_name = obj_array.terms.category[0].name
And That does the trick.
Maybe it's too late for this answer, but I hope someone with the same problem will find this as I did before finding the Solution :)
I struggled with this issue today, and thought I'll leave a reply with my solution.
I was fetching a data object via ajax, something like this:
{"constants": {"value1":"x","value2":"y"},"i18n" {"data1":"x", "data2":"y"}}
Let's say this object is in a variable called data. Whenever I referenced data.i18n
I got undefined
.
console.log(data)
showed the object as expectedconsole.log(Object.keys(data))
said ["constants","i18n"]
as expectedNothing helped... Then on the server side I wrote the data to the php log, and it revealed this:
{"constants": {"value1":"x","value2":"y"},"\u045618n" {"data1":"x", "data2":"y"}}
The "i" in the index key was actually a u0456 (cyrillic i). This was not visible in my php editor or the browser console log. Only the php log revealed this... That was a tricky one...
In 2018 Mozilla warns us in the Mozilla Docs here!
I quote "Logging Objects":
Don't use
console.log(obj);
, useconsole.log(JSON.parse(JSON.stringify(obj)));
.This way you are sure you are seeing the value of obj at the moment you log it.
I've just had this issue with a document loaded from MongoDB using Mongoose.
When running console.log()
on the whole object, all the document fields (as stored in the db) would show up. However some individual property accessors would return undefined
, when others (including _id
) worked fine.
Turned out that property accessors only works for those fields specified in my mongoose.Schema(...)
definition, whereas console.log()
and JSON.stringify()
returns all fields stored in the db.
Solution (if you're using Mongoose): make sure all your db fields are defined in mongoose.Schema(...)
.
My data was just json data string . (This variable was stored as json string in the session).
console.log(json_string_object)
-> returns just the representation of this string and there is no way to make difference whether is string or object.
So to make it work I just needed to convert it back to real object:
object = JSON.parse(json_string_object);