Wednesday, 15 March 2017

Mongoose populate single document after query

Using, Mongoose and Passport, when I reach my controller, I have the logged user accessible through req.user;

I'd like to populate this user with an association.

I could write:

Fragment.find({_id: {$in: req.user.fragments}}).then(fragments => {
  req.user.fragments = fragments;
});

I could also move this to a custom method in the model, but both solutions don't feel like the correct way to do this.

How I can write req.user.populate('fragments').then(userWithFragments => {});?



via Augustin Riedinger

No comments:

Post a Comment