Recently, I ran into an issue with an undefined error when trying to access a value in a JavaScript object key. I retrieved arrays of objects using the mongoose.find().exec()
callback and then checked each object for a specific key.
Here is an example object:
{"promo_id":"5af1c07d5542fc9307847a6a",
"__v":0,
"description":"Hello",
"available_count":0}
- When I tested
, it unexpectedly returnedobj.hasOwnProperty('available_count')
false
. - Using
lodash
's_.isObject()
method on the object returned true. - Ultimately, I had to resort to
JSON.parse(JSON.stringify(obj))
to resolve the issue.
I am perplexed by this strange behavior and would appreciate any insights on why this is happening.
let o = {"promo_id":"5af1c07d5542fc9307847a6a",
"__v":0,
"description":"Hello",
"available_count":0}
let result = o.hasOwnProperty('available_count');
console.log(result)