Pranav Kotecha
Pranav Kotecha

Reputation: 85

mongoose db populate doesn't seem to work

So this is how my models are:

groupModel.js

var Schema = mongoose.Schema;
var groupSchema = new Schema({
  uuid:         String,
  users:        [{type: Schema.Types.ObjectId, ref:'users'}]
});
var Groups = mongoose.model('groups', groupSchema);
module.exports = Groups;

userModel.js

var mongoose = require('mongoose')
var Schema = mongoose.Schema;

var userSchema = new Schema({
  username:         String,
  password:         String,
  firstname:        String,
  lastname:         String,
  email:            String,
  emailVerified:    Boolean
});

var Users = mongoose.model('users', userSchema);
module.exports = Users;

and this is the api I am hoping to get it to work:

/* GET /group/list/:id listing. */
router.get('/list/:id', function(req, res) {
    Group.findOne({uuid : 
req.params.id}).populate('Users').exec(function(err, group){
    if(err) throw err;
    console.log(group);
    res.status(200).send(group);
});
});

this is the response I get back:

{"_id":"5aaf52b4165e97aae4a0b42c","uuid":"iyzIc","__v":0,"users": 
["58f5acae4733ae5f64XXXXea","590a663c2a32ad28e0XXXX29"]}

For some reason I am not able to replace the id's with actual data for the users. I was hoping to get user related details instead of ids for the two users. Am I doing something wrong? I am trying to learn nodejs and was hoping some nodejs expert will be able to find my silly mistake.

EDITED: changing .populate('Users') -> .populate('users') fixed the issue.

Upvotes: 0

Views: 398

Answers (1)

Tetsuya3850
Tetsuya3850

Reputation: 1019

Users being capitalized in populate is the problem!

Upvotes: 2

Related Questions