Tag Archives: mongoose

Mongoose: Cannot read property ‘paths’ of undefined

When working with Mongoose, make sure you never ever name a property “type”, since it’s reserved for declaring datatypes on fields. Take a look at this:

permissions: {
  type: String,   // this will throw an error
  userGroups: [
    {
      group: { type: mongoose.Schema.Types.ObjectId, ref: 'UserGroups' },   // correct use of "type"
      permission: String
    }
  ]
}

This will throw an uncaught exception. I’m writing this post so I can save you 20 minutes of debugging. Here’s the correct approach:

permissions: {
  permissionType: String,   // correct usage
  userGroups: [
    {
      group: { type: mongoose.Schema.Types.ObjectId, ref: 'UserGroups' },
      permission: String 
    }
  ]
}