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

后端 未结 30 2078
日久生厌
日久生厌 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:17

    I also had this frustrating problem, I tried the setTimeout() and the JSON.stringify and JSON.parse solutions even if I knew it wouldn't work as I think they're mostly JSON or Promise related problems, sure enough it didn't work. In my case though, I didn't notice immediately that it was a silly mistake. I was actually accessing a property name with a different casing. It's something like this:

    const wrongPropName = "SomeProperty"; // upper case "S"
    const correctPropName = "someProperty"; // lower case "s"
    const object = { someProperty: "hello world!" };
    
    console.log('Accessing "SomeProperty":', object[wrongPropName]);
    console.log('Accessing "someProperty":', object[correctPropName])

    It took me a while to notice as the property names in my case can have either all lower case or some having mixed case. It turned out that a function in my web application has something that makes a mess of my property names (it has a .toLowerCase() next to the generated key names

提交回复
热议问题