-1

Extremely weird occurence. Manually create an object, then call Object.keys on it - keys are found. If you try to access that value via the property key name, 'undefined'.

Example:

let obj = {
    a : 'Foo',
    b : 'Bar'
}

console.log(Object.keys(obj));
// a,b

console.log(Object.getOwnPropertyNames(obj));
// a,b (for the non ES6 folks among us)

console.log(obj.a);
// undefined

console.log(obj['a']);
// undefined 

Kind of left scratching my head on this one.

NodeJS Version LTS 10.15.3

NPM Version 6.9.0

georg
  • 211,518
  • 52
  • 313
  • 390
Wes
  • 467
  • 3
  • 11
  • 1
    You are missing `'` at end of `'Bar`. And also its not logging `undefined` its logging `Foo` – Maheer Ali Mar 27 '19 at 12:22
  • I'd say you're running into [this problem](http://stackoverflow.com/questions/38660832/element-children-has-elements-but-returns-empty-htmlcollection) but you say that you're using Node.js, not a browser, and Node.js's standard console doesn't do this. (If you're debugging Node.js via devtools, it **does** have that behavior.) – T.J. Crowder Mar 27 '19 at 12:24
  • 1
    I have a [certain suspicion](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call), or are you saying the above code reproduces this? Keep in mind that `console.log` calls in the browser display live outputs which are updated when the objects change –  Mar 27 '19 at 12:25
  • The only other thing I can imagine is that the property is actually called `a,b`: `let obj = {"a,b": "something"};` `["a,b"].toString()` and `["a", "b"].toString()` both produce the string `"a,b"`. – T.J. Crowder Mar 27 '19 at 12:25
  • If you're using a specific platform such as react native this problem may occur. I have observed that `Object.values()` does not work with react native. – kemicofa ghost Mar 27 '19 at 12:28
  • @kemicofa - I find that **extremely** hard to believe. I think something else must have been going on. I could imagine it not being there, but I'm certain it isn't there but nonfunctional, unless someone's installed a poor polyfill of course. – T.J. Crowder Mar 27 '19 at 12:28
  • The only way I can see to reproduce this: 1. use a live console 2. run the four `.log()` calls 3. set the object's `a` and `b` - this will reproduce what you describe, since the first two logs are updated when the object changes: [live example](https://jsfiddle.net/khrismuc/yk42dzce/) –  Mar 27 '19 at 12:29
  • 1
    @T.J.Crowder - You're right. I actually just added my own answer after figuring out what the problem was. Apparently, there was a hidden character that had made it's way in that wasn't visible in the code editor. I deleted all the code, wrote it again from scratch, and it worked. Doh! – Wes Mar 27 '19 at 13:00

2 Answers2

0

Found the answer to this one after investigating it further.

There appears there was a hidden character in my key definitions that was visually invisible in the code editor, but preventing me from accessing it by key name.

I deleted all the code, wrote it again from scratch, and now it works as expected.

Weird happenstance - I knew I couldn't have been the guy who discovered a bug like this, but it was super bazaar!

Wes
  • 467
  • 3
  • 11
-1

While creating an object, value 'Bar' is missing single quote in it's declaration.

You have:

let obj = {
   a : 'Foo',
   b : 'Bar
}

And correct is

let obj = {
    a : 'Foo',
    b : 'Bar'
}
  • While this is true it's most likely a copy-paste issue; if OP didn't have that closing quote they'd get a complete different result –  Mar 27 '19 at 12:32