0
votes

I'm using mongoose 4.6.6, express 4.13, passport 0.3.

I have the next mongoose Schema

var userSchema = new Schema({
  nombre: String,
  apellidos: String,
  email: String,
  pass: String,
  fecha_registro : { type: Date, default: Date.now },
  rol_list: [Schema.Types.ObjectId], // generic array of objectId
  deleted: {type: Boolean, default: false}
});

module.exports = mongoose.model('User', userSchema);

When I search a user and try to populate the "rol_list" array, is always empty. I have looked in mongo the users are well filled, but mongoose return it empty.

passport.deserializeUser(function(id, done) {
  User.findById(id)
    .populate('rol_list')
    .exec(function(err, user) {
      console.log(user);
      done(err, user);
    });
});

The console.log(user) show always the array rol_list empty.

If I assign a reference to the ObjectId like:

rol_list: [{ type: Schema.Types.ObjectId, ref: 'Rol1' }]

than is correct filled, logically only with the element "Rol1".

Any idea?

2
What is the name of the schema behind rol_list?Orelsanpls
no, no have schema behind. I want a generic array of objects.Raugaral

2 Answers

0
votes

There is an option in .populate(...) mongoose function that allow you to specify the model that's behind the ObjectId.

@example

Conversation.find().populate('creator', null, 'User2').exec(callback);

Stack overflow post: mongoose-populate-field-without-ref-option

-1
votes

If you want array of only object ids then don't use populate with it.

like:

passport.deserializeUser(function(id, done) {
  User.findById(id)
    .exec(function(err, user) {
      console.log(user);
      done(err, user);
    });
});