I've got an object that represents a user. The object has the users's comments, spots, etc. stored as an array When I populate the user I get the comments, spots, etc. HOWEVER inside the comments, spots, etc. there are more fields
user: { __v: 0,
_id: 531f36ccccf511243bf9e0dc,
avatar: '',
created: Thu Mar 13 2014 14:33:05 GMT+0000 (GMT),
lastUpdated: Thu Mar 13 2014 21:59:06 GMT+0000 (GMT),
currentLocation:
{ _id: 5381eff2b3931c953a000133,
position: { Long: -2.402307, Lat: 53.472664 },
information: '<p>Rather nice, actually</p>',
rating: 5,
placename: 'little bridge',
site: 'Barton Moss',
spots: [ 5315f8b384119cb4362797f4 ] },
points: 1,
username: 'Jim the Twitcher',
google: {},
twitter: {},
facebook: {},
local:
{ password: '***',
email: '***' },
spots:
[ { lastUpdated: Tue Mar 04 2014 16:00:51 GMT+0000 (GMT),
created: Tue Mar 04 2014 16:00:51 GMT+0000 (GMT),
user: 531f36ccccf511243bf9e0dc,
bird: 5315f8b384119cb4362797f6,
location: 5381eff2b3931c953a000133,
date: Tue Mar 04 2014 00:00:00 GMT+0000 (GMT),
time: '16:00:51.574Z',
_id: 5315f8b384119cb4362797f4,
__v: 0 } ],
ranking: { position: 1, total: 2 },
comments:
[ { lastUpdated: Thu Mar 13 2014 22:38:38 GMT+0000 (GMT),
created: Thu Mar 13 2014 22:38:38 GMT+0000 (GMT),
comment: 'mum mum',
source: 'BirdsApp',
user: 531f36ccccf511243bf9e0dc,
_id: 5322336e6db84e983355c31a,
__v: 0 } ] }
If you look at 'spots' you can see that the 'user', 'bird' and 'location' fields aren't being populated.
process.nextTick(function() {
User.findOne({
'local.email': email
}).populate({
path: 'spots comments currentLocation'
}).exec(function(err, user) {
// console.log('first user: ', user);
// console.log('actual user');
// if there are any errors, return the error
if (err)
return done(err);
// if no user is found, return the message
if (!user)
return done(null, false, req.flash('loginMessage', 'No user found.'));
if (!user.validPassword(password))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));
// all is well, return user
else
var opts = {
path: 'spots.bird',
model: 'Bird'
};
User.populate(user, opts, function(err, user) {
if (err)
return done(err);
else
var opts = {
path: 'spots.location',
model: 'Location'
};
User.populate(user, opts, function(err, user) {
if (err)
return done(err);
else
//console.log('new user: ', user);
console.log(util.inspect(user, showHidden = false, depth = 5, colorize = true));
return done(null, user);
});
});
});
});
...is what I've tried so far. I know deep population is an issue in mongoose (and tbh I struggle to understand mongoose conceptually on some levels). Am I doing this right, or do I have to somehow populate the object I receive in the exec callback?
Any help would be very much appreciated...
Thanks
James
Edit: The population seems to be working fine, but as I mentioned on the reply from @JohnnyHK, I think somehow it's delivering promise objects as when I come to print them in the dustjs sheets I'm using I still just get the objectID, even though I can see them in the console...