Can't access object property, even though it shows up in a console log

后端 未结 30 2298
日久生厌
日久生厌 2020-11-22 06:26

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\

30条回答
  •  情书的邮戳
    2020-11-22 07:08

    In my case I was passing an object to a promise, within the promise I was adding more key/values to the object and when it was done the promise returned the object.

    However, a slight over look on my part, the promise was returning the object before it was fully finished...thus the rest of my code was trying to process the updated object and the data wasn't yet there. But like above, in the console, I saw the object fully updated but wasn't able to access the keys - they were coming back undefined. Until I saw this:

    console.log(obj) ;
    console.log(obj.newKey1) ;
    
    // returned in console
    > Object { origKey1: "blah", origKey2: "blah blah"} [i]
        origKey1: "blah"
        origKey2: "blah blah"
        newKey1: "this info"
        newKey2: "that info"
        newKey3: " more info"
    > *undefined*
    

    The [i] is a little icon, when I hovered over it it said Object value at left was snapshotted when logged, value below was evaluated just now. Thats when it occurred to me that my object was being evaluated before the promise had fully updated it.

提交回复
热议问题