Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't retrieve object property in Javascript

Currently, I'm trying to retrieve a particular property from a javascript object returned by mongoose. I can see the property when printing the whole object, but when trying to use it, I get an undefined. Here's the code:

Match.findOne({id: match.id}, function (err, match) {
    console.log(match)
    console.log(typeof match)
    console.log(match.id)
    console.log(match.winner)
})

The output of that is:

{ _id: 552c7f2925efcbc88ccc55bf,
    id: 499142595,
    region: 'br',
    winner: 200,
    championsLose: [ 25, 96, 81, 113, 63 ],
    championsWin: [ 37, 238, 201, 268, 81 ] 
}
object
499142595
undefined

Even though the 'winner' property is clearly there. Any ideas?

UPDATE: added more logging to the above code and results of it

like image 727
Alejandro Baltra Avatar asked Feb 02 '26 19:02

Alejandro Baltra


1 Answers

It's odd that there are no quotes around the value of associated with the key "_id":

{ _id: 552c7f2925efcbc88ccc55bf, // should be --> "552c7f2925efcbc88ccc55bf" 
id: 499142595,
region: 'br',
winner: 200,
championsLose: [ 25, 96, 81, 113, 63 ],
championsWin: [ 37, 238, 201, 268, 81 ] 
}

This seems to indicate that it is not actually an object. Which, of course, explains why match.winner is undefined. I would do console.log(typeof match) and see what it says.

like image 191
dave Avatar answered Feb 05 '26 08:02

dave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!