node.js - Why won't my refs populate documents? -


i using mongoose 4.3.6 in node.js app.

say, example, have 2 models:

var user = mongoose.model('user', someparentschema.extend({   _id: string,   name: string }));  var = mongoose.model('like', anotherparentschema.extend({   _id: string,   user_id: {     type: string,     ref: 'user'   } })); 

i using mongoose-schema-extend, , can sake of question both entities extend parent schema.

since using ref, in theory model.populate should auto-populate field user model.

the ref option tells mongoose model use during population

like.findone({}).populate('user_id').exec(callback); 

the above never works. simple (the field ends null). have make work:

like.findone({}).populate({ path: 'user_id', model: 'user' }).exec(callback); 

when that, field expected user object (but reason had explicitly tell populate path user model; though ref supposed handle that, according mongoose docs linked above).

am missing something?

i test sample codes following, query like.findone({}).populate('user_id').exec(callback); seems work well.

schema:

var personschema = new mongoose.schema({     t: string }, {collection: 'persons'});  var user = mongoose.model('user', personschema.extend({   _id: string,   name: string }));  var parentschema = new mongoose.schema({     s: string }, {collection: 'parent'});  var = mongoose.model('like', parentschema.extend({   _id: string,   user_id: {     type: string,     ref: 'user'   } })); 

insert data db,

var user = new user({     t: 't1',     _id: '1234567',     name: 'test' });  var = new like({     s: 's1',     _id: '23456789', });  user.save(function(err, u){     if(err)         console.log(err);     else {         like.user_id = u._id;         console.log(like);         like.save(function(err) {             if (err)                 console.log(err);             else                 console.log('save , user....');         });     } }); 

query

like.findone({}).populate('user_id').exec(function(err, doc) {     if (err)         console.log(err);     else         console.log(doc); }); 

and result

{ _id: '23456789',   __t: 'like',   user_id: { _id: '1234567', __t: 'user', t: 't1', name: 'test', __v: 0 },   s: 's1',   __v: 0 } 

Comments