{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/4071","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/4071/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/4071/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/4071/events","html_url":"https://github.com/Automattic/mongoose/issues/4071","id":148043593,"number":4071,"title":"TypeError: Cannot read property 'apply' of undefined","user":{"login":"SimoneTaliercio","id":3648619,"avatar_url":"https://avatars.githubusercontent.com/u/3648619?v=3","gravatar_id":"","url":"https://api.github.com/users/SimoneTaliercio","html_url":"https://github.com/SimoneTaliercio","followers_url":"https://api.github.com/users/SimoneTaliercio/followers","following_url":"https://api.github.com/users/SimoneTaliercio/following{/other_user}","gists_url":"https://api.github.com/users/SimoneTaliercio/gists{/gist_id}","starred_url":"https://api.github.com/users/SimoneTaliercio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SimoneTaliercio/subscriptions","organizations_url":"https://api.github.com/users/SimoneTaliercio/orgs","repos_url":"https://api.github.com/users/SimoneTaliercio/repos","events_url":"https://api.github.com/users/SimoneTaliercio/events{/privacy}","received_events_url":"https://api.github.com/users/SimoneTaliercio/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-04-13T12:26:27Z","updated_at":"2016-04-16T21:37:27Z","closed_at":null,"body":"Hi all : )\r\n\r\nMy project contains a multi-level Schema structure (A <-B <-C). We achieved that level of inheritance thanks to a third party module called mongoose-schema-extend.\r\n\r\nEverything was fine with mongoose 3.x but we are migrating now to 4.x . Unfortunately we get the following exception:\r\n\r\n`TypeError: Cannot read property 'apply' of undefined at q.reduce.post (/Users/simone/Enviroments/nodejs/target-manager-server/node_modules/mongoose/lib/document.js:1764:21)\r\n      at Array.reduce (native)\r\n      at model.Document.$__registerHooksFromSchema (/Users/simone/Enviroments/nodejs/target-manager-server/node_modules/mongoose/lib/document.js:1762:18)\r\n      at model.Document (/Users/simone/Enviroments/nodejs/target-manager-server/node_modules/mongoose/lib/document.js:79:8)\r\n      at model.Model (/Users/simone/Enviroments/nodejs/target-manager-server/node_modules/mongoose/lib/model.js:42:12)\r\n      at new model (/Users/simone/Enviroments/nodejs/target-manager-server/node_modules/mongoose/lib/model.js:3096:11)\r\n      at Object.createIm`\r\n\r\n\r\nI had a look at mongoose/lib/document.js line 1762 and I found out that commenting the IF block in the following code then would fix the issue.\r\n\r\n`// we are only interested in 'pre' hooks, and group by point-cut\r\n  var toWrap = q.reduce(function(seed, pair) {\r\n    if (pair[0] !== 'pre' && pair[0] !== 'post' && pair[0] !== 'on') {\r\n      _this[pair[0]].apply(_this, pair[1]);\r\n      return seed;\r\n    }`\r\n\r\nWhat does that code ? Any workaround I can use from my application in order to be compliant with that check ?\r\n\r\nThanks a lot for your support! :_)"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/210905954","html_url":"https://github.com/Automattic/mongoose/issues/4071#issuecomment-210905954","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/4071","id":210905954,"user":{"login":"vkarpov15","id":1620265,"avatar_url":"https://avatars.githubusercontent.com/u/1620265?v=3","gravatar_id":"","url":"https://api.github.com/users/vkarpov15","html_url":"https://github.com/vkarpov15","followers_url":"https://api.github.com/users/vkarpov15/followers","following_url":"https://api.github.com/users/vkarpov15/following{/other_user}","gists_url":"https://api.github.com/users/vkarpov15/gists{/gist_id}","starred_url":"https://api.github.com/users/vkarpov15/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkarpov15/subscriptions","organizations_url":"https://api.github.com/users/vkarpov15/orgs","repos_url":"https://api.github.com/users/vkarpov15/repos","events_url":"https://api.github.com/users/vkarpov15/events{/privacy}","received_events_url":"https://api.github.com/users/vkarpov15/received_events","type":"User","site_admin":false},"created_at":"2016-04-16T21:37:27Z","updated_at":"2016-04-16T21:37:27Z","body":"Honestly mongoose-schema-extend is a broken module and it's a minor miracle that it works at all. Schema inheritance is much easier if you just use ES2015 class syntax:\r\n\r\n```javascript\r\nclass PersonSchema extends mongoose.Schema {\r\n  constructor(obj, options) {\r\n    super(obj, options);\r\n    this.add({ name: String });\r\n  }\r\n}\r\n\r\nclass EmployeeSchema extends PersonSchema {\r\n  constructor(obj, options) {\r\n    super(obj, options);\r\n    this.add({ department: String });\r\n  }\r\n}\r\n\r\nconst employeeSchema = new EmployeeSchema();\r\nconst Employee = mongoose.model('Employee', employeeSchema);\r\n\r\nconsole.log(new Employee({ name: 'Val', department: 'test' }));\r\n```"}}