I have stumbled on a weird issue when trying to recursively set properties on an empty object with the following code:
Simplified code
const birthdays = {};
// Loop -> Passing day, id and birthday
birthdays[day] = day;
birthdays[day][id] = birthday;
Example of day: '01012017'
Example of id: 1547
Example of birthday: {name: John}
Error message
Cannot create property '123' on string '06012017'
I saw some people with Angular having this issue but their answer don't solve anything for me (as is angular specific syntax etc).
Empty Objects need to be individually created before their values are assigned. And use of const
is not a good idea here, anyway, it's just my suggestion.
const birthdays = {};
var day = 123;
var id = 21;
var birthday = 2016;
// Loop -> Passing day, id and birthday
birthdays[day] = {};
birthdays[day][id] = birthday;
I had a similar error message TypeError: Cannot create property 'false' on boolean 'false'
in Node 14.3.0 (ES 2019), using the new Javascript semicolon-less syntax, with a variable declaration and assignment followed by a destructuring assignment on the next line.
The code that caused the error:
var a = b ()
[c, d] = e ()
Solved by:
var a = b ()
;
[c, d] = e ()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With