Encountering an issue while trying to access a property of a JSON object directly from a MongoDB document, leading to an undefined value. Surprisingly, the use of JSON.stringify() generates a value for that specific property. Is there a way to access the property without resorting to JSON.parse(JSON.stringify())?
The following code snippet demonstrates the problem:
console.log(p.isFinal);
console.log(p.playoffType);
console.log(JSON.parse(JSON.stringify(p)).playoffType);
The expected output should be:
false
seed
seed
It's worth noting that in the example, p =
{ _id: 5da0eef8d7772b13dc58d2e1,
week: 14,
isFinal: false,
isPlayoff: true,
playoffType: 'seed',
playoffTeams: [ 4, 5 ],
teams: [] }
Furthermore, when initializing p as an object literal, direct property access works flawlessly. The unexpected behavior only arises when p is retrieved from a MongoDB query.
This discrepancy seems to suggest a unique storage format within the MongoDB call, where properties are not correctly recognized until coerced through JSON.parse(JSON.stringify(p)).
Any insights or solutions to this dilemma would be greatly appreciated!