mongoose db populate似乎不起作用

时间:2018-03-20 21:58:51

标签: node.js mongoose mongoose-populate

所以这就是我的模型:

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;

这是api我希望能让它发挥作用:

/* 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);
});
});

这是我回复的回复:

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

由于某些原因,我无法用用户的实际数据替换id。我希望得到两个用户的用户相关细节而不是ID。难道我做错了什么?我正在尝试学习nodejs,并希望一些nodejs专家能够找到我的愚蠢错误。

EDITED: 改变.populate('用户') - > .populate('用户')解决了这个问题。

1 个答案:

答案 0 :(得分:1)

在填充中大写的用户是问题!