3

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

  • Can you include a [minimal, complete and verifiable example](http://stackoverflow.com/help/mcve) as a fiddle or snippet? – adamdc78 Apr 15 '15 at 21:18
  • ^ fiddle for mongdb/mongoose? – A.B Apr 15 '15 at 21:18
  • 1
    Perhaps you have a virtual defined on a model somewhere, and it's fooling you? – nullpotent Apr 15 '15 at 21:19
  • 2
    The problem is somewhere else; this code looks fine to me. I'd replace "match" with another variable name and see how that affects it because I think there might be a problem with a global that's referenced with the same name. – frenchie Apr 15 '15 at 21:19
  • are you changing deleting match.winner afterwards? it reflects back – A.B Apr 15 '15 at 21:20
  • @A.B can hardcode the data in the post for the MCVE, though if it's a DB configuration issue then a fiddle wouldn't make sense (does mongoose use lazy-loading like hibernate?). – adamdc78 Apr 15 '15 at 21:21
  • 1
    I'm not convinced the `console.log(match)` is outputting an object - that's why `match.winner` is undefined – CᴴᵁᴮᴮʸNᴵᴺᴶᴬ Apr 15 '15 at 21:24
  • The two `console.log` calls are really located right next to each other? – Felix Kling Apr 15 '15 at 21:29
  • Yes, they are next to each other. I'll print the typeof in a sec, I'll update with the info. I tried changing the name of the key in mongo and printing again, but the issue persisted. Not sure how to make a fiddle of it :( – Alejandro Baltra Apr 16 '15 at 02:01
  • woot woot! cookies for @iccthedral The problem was in the model definition – Alejandro Baltra Apr 16 '15 at 02:19

3 Answers3

2

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.

dave
  • 62,300
  • 5
  • 72
  • 93
0

console updates("Live Update") the last references if the object/property is change even after console.log(). i have recently got this pain using chrome

it might be the case

check this example in chrome, dont know how this problem behaves in node console or the tool you are using

obj = {a:"b",b:"c"};
console.log(obj);  // logs Object { b: "c" } not {a:"b",b:"c"}
delete obj.a;
console.log(obj);// logs Object { b: "c" }

you might have deleted the match.winner property later in the code. that seems to be the case only

A.B
  • 20,110
  • 3
  • 37
  • 71
0

Finally the issue wasn't in that piece of code, but in the Model definition. The variable name wasn't updated properly, so the old name was still being used there:

var mongoose = require('mongoose');

module.exports = mongoose.model('Match',{
    id: Number,
    region: String,
    championsWin: Array,
    championsLose: Array,
    winnerTeamId: Number
});

And the proper model should be:

module.exports = mongoose.model('Match',{
    id: Number,
    region: String,
    championsWin: Array,
    championsLose: Array,
    winner: Number
});

Thanks a lot for the help!