dimanche 19 avril 2015

mongoose deep populate based on query

I'm populating the mongoose model using deepPopulate plugin like so:



User.findOne({_id: req.params.user_id})
.deepPopulate('classes.assignments.submissions')
.exec(function (err, user) {
res.send(user);
})


And I want the submissions to be populated based on the query - only submissions for a current user - something like .where(submissions.user == req.params.user_id). I can't find the solution for this and end up in a situation where current user can see submissions from other users.


I was trying to set submissions for other users to null manually inside the exec:



for (var i = 0; i < user.classes.length; i++) {
var cl = user.classes[i];
for (var i = 0; i < cl.assignments.length; i++) {
var assign = cl.assignments[i];
for (var i = 0; i < assign.submissions.length; i++) {
var subm = assign.submissions[i];
if(subm.user != req.params.user_id){
subm = null
};
};
};
};


But it still returns not filtered submissions.


Aucun commentaire:

Enregistrer un commentaire