Reputation: 19
OK so I need help! I've been stuck on this for a week now and I have no clue what to do! So basically in my app I'm trying to display a list of users based on their user type. (eg. student, teacher etc.) I can display all the users but not by the specific user type.
Here is an extract of my code for the route to the teacher list.
//route for teacher list
router.get('/teacherlist',function (req,res,next) {
User.find(function(err, teachers) {
res.render('user/userList', {title: 'user list',teachers:teachers});
});
});
teacherlist is where I want to direct the page and that works no problem, its the user.find line that I think I'm going wrong. I know I have declare that I want to display the usertype = teacher, I just am not sure how to do that.
Thank you. :)
Upvotes: 0
Views: 2194
Reputation: 10864
In case you want to retrieve users of type teacher
or student
, you can try the following:
router.get('/teacherlist',function (req,res,next) {
User.find({ $or: [ { usertype: 'teacher' }, { usertype: 'student' } ] }, function(err, teachers) {
if(err) {
next(err);
} else {
res.render('user/userList', {title: 'user list',teachers:teachers});
}
});
});
And if you want just a particular user type you could use @Marco
's answer.
Upvotes: 1
Reputation: 2395
Are you using Moongoose for database access?
Here is how you find documents: http://mongoosejs.com/docs/api.html#model_Model.find
//route for teacher list
router.get('/teacherlist',function (req,res,next) {
User.find({usertype: 'teacher'}, function(err, teachers) {
if(err) {
next(err); //pass your error to error handler
} else {
res.render('user/userList', {title: 'user list',teachers:teachers});
}
});
});
Upvotes: 0