niahmiah / mongoose-uuid

uuid type for mongoose
21 stars 24 forks source link

Problems with .findById() and .Populate() with mongoose 4.5.9 #4

Closed dtoubelis closed 7 years ago

dtoubelis commented 8 years ago

mongo: 3.2.8 mongoose: 4.5.9

I'm trying to use the mongoose-uuid2 package for primary keys in my database and here is my schema:

require('mongoose-uuid2').loadType(mongoose);
const UUID = mongoose.Types.UUID;

...

  let emailSchema = new Schema({
    _id: { type: UUID, default: uuid.v4 },
    email: { type: String, required: true, lowercase: true, trim: true, unique: true },
    confirmed: { type: Date },
    _owner: { type: UUID, ref: 'User' }
  }, {
    safe: true,
    strict: true,
    timestamps: true
  });

  let userSchema = new Schema({
    _id: { type: UUID, default: uuid.v4 },
    first_name: { type: String },
    last_name: { type: String },
    password: { type: String, required: true },
    _primary_email: { type: UUID, ref: 'Email' }
  }, {
    safe: true,
    strict: true,
    timestamps: true
  });

I managed to populate references saving id directly and I can see all correct entries in my database. However, if I do something like this:

models.User.findById(userId).exec().then(function(userModel) {
    console.log(userModel);
});

I can see this output:

{ _id: 
   Binary {
     _bsontype: 'Binary',
     sub_type: 4,
     position: 16,
     buffer: <Buffer 22 12 2d 07 91 ed 4c 4a 8a 41 03 da 59 a9 e1 2c> },
  updatedAt: 2016-08-25T18:23:27.103Z,
  createdAt: 2016-08-25T18:23:24.962Z,
  password: '$2a$10$ZSjEv98fMYsmQ6NKA2YrBetaUSaoNJ2q3Q5mAL8g/j7hHXH0XXG7G',
  first_name: 'Joe',
  last_name: null,
  _primary_email: 
   Binary {
     _bsontype: 'Binary',
     sub_type: 4,
     position: 16,
     buffer: <Buffer c2 6d 91 b3 29 f1 4c 3c 8f d8 9e 1e 85 41 91 35> },
  __v: 0
}

which is correct. Next, I modify the query to populate _primary_email field like so:

models.User.findById(userId)populate('_primary_email')..exec().then(function(userModel) {
    console.log(userModel);
});

I get this object:

{ _id: 
   Binary {
     _bsontype: 'Binary',
     sub_type: 4,
     position: 16,
     buffer: <Buffer 22 12 2d 07 91 ed 4c 4a 8a 41 03 da 59 a9 e1 2c> },
  updatedAt: 2016-08-25T18:23:27.103Z,
  createdAt: 2016-08-25T18:23:24.962Z,
  password: '$2a$10$ZSjEv98fMYsmQ6NKA2YrBetaUSaoNJ2q3Q5mAL8g/j7hHXH0XXG7G',
  first_name: 'Joe',
  last_name: null,
  __v: 0
}

as you can see, any references to primary email is gone and it bacame undefined. The similar thing happens if I try to assign object to _primary_email field, so this code does not work either:

models.User.findById(userId).exec().then(function (userModel) {
    if (!userModel) {
      throw new errors.UserNotFoundError();
    }
    models.Email.findById(userModel._primary_email).exec().then(function(emailModel) {
        if (!emailModel) {
          throw new errors.NotFoundError();
        }
        userModel._primary_email = emailModel;
        console.log(props.userModel);
    });
})

I get:

{ _id: 
   Binary {
     _bsontype: 'Binary',
     sub_type: 4,
     position: 16,
     buffer: <Buffer 22 12 2d 07 91 ed 4c 4a 8a 41 03 da 59 a9 e1 2c> },
  updatedAt: 2016-08-25T18:23:27.103Z,
  createdAt: 2016-08-25T18:23:24.962Z,
  password: '$2a$10$ZSjEv98fMYsmQ6NKA2YrBetaUSaoNJ2q3Q5mAL8g/j7hHXH0XXG7G',
  first_name: 'Joe',
  last_name: null,
  _primary_email: 
   Binary {
     _bsontype: 'Binary',
     sub_type: 4,
     position: 16,
     buffer: <Buffer c2 6d 91 b3 29 f1 4c 3c 8f d8 9e 1e 85 41 91 35> },
  __v: 0
}

So, it is seen from this example the _primary_email filed still showing model _id instead of the actual object.

I tested this logic with ObjectId instead of UUID and it seems work as expected, so I think there may be something special about using UUID as a primary key. Any ideas?

niahmiah commented 7 years ago

This should all work correctly now, as of 1.2.0. Thanks!