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\
I faced the same problem today. In my case the keys were nested, i.e key1.key2. I split the keys using split() and then used the square bracket notation, which did work for me.
var data = {
key1: {
key2: "some value"
}
}
I split the keys and used it like this, data[key1][key2] which did the job for me.
I had the same issue and no solution above worked for me and it sort of felt like guess work thereafter. However, wrapping my code which creates the object in a setTimeout
function did the trick for me.
setTimeout(function() {
var myObj = xyz; //some code for creation of complex object like above
console.log(myObj); // this works
console.log(myObj.propertyName); // this works too
});
None of the JSON stringify/parse worked for me.
formValues.myKey: undefined
formValues.myKey with timeout: content
I wanted the value of formValues.myKey
and what did the trick was a setTimeout 0 like in the example below. Hope it helps.
console.log('formValues.myKey: ',formValues.myKey);
setTimeout( () => {
console.log('formValues.myKey with timeout: ', formValues.myKey);
}, 0 );
The property you're trying to access might not exist yet. Console.log works because it executes after a small delay, but that isn't the case for the rest of your code. Try this:
var a = config.col_id_3; //undefined
setTimeout(function()
{
var a = config.col_id_3; //voila!
}, 100);
I had an issue like this, and found the solution was to do with Underscore.js. My initial logging made no sense:
console.log(JSON.stringify(obj, null, 2));
> {
> "code": "foo"
> }
console.log(obj.code);
> undefined
I found the solution by also looking at the keys of the object:
console.log(JSON.stringify(Object.keys(obj)));
> ["_wrapped","_chain"]
This lead me to realise that obj
was actually an Underscore.js wrapper around an object, and the initial debugging was lying to me.
In My case, it just happens to be that even though i receive the data in the format of a model like myMethod(data:MyModelClass)
object till the received object was of type string.
Which is y in console.log(data) i get the content.
Solution is just to parse the JSON(in my case)
const model:MyMOdelClass=JSON.parse(data);
Thought may be usefull.