How to access specific value from mongoose query callback?

Viewed 12971

First a little introduction to the situation. I have a MongoDB collection filled with documents. I use a schema.statics to query a particular row

TweetSchema.statics.maxAndLimit = function(screen_name, cb) {
  this.find({
    'user.screen_name': screen_name
  }).sort({'id_str':1}).select({'id_str':1,'user.statuses_count':1,'user.screen_name':1,'_id':0}).limit(1).exec(cb);
};

When the query is finished it calls the callback (cb).

In the callback I want to bind the values to variables so I can use them later. This is what I can't seem to solve:

console.log(result) == [{id_str:'12346875',user:{statuses_count:500,screen_name:'username'}}]

console.log(result.id_str) == 'undefined'

Same goes for:

console.log(result[0].id_str)

Why can I not get a specific value? The typeof(result) says 'object'.

Update per request My non-strict schema caused Mongoose to return a non-real javascript object. So for future reference here's the 'schema' I used:

var TweetSchema = new Schema({}, {strict: false});

I didn't want to define everything as it's a Twitter Timeline object and thus not always the same.

1 Answers
Related