{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/3669","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/3669/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/3669/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/3669/events","html_url":"https://github.com/Automattic/mongoose/issues/3669","id":122063110,"number":3669,"title":"Mongoose + Bluebird: invalidate() causes \"Unhandled rejection\" warning","user":{"login":"geronime","id":1003724,"avatar_url":"https://avatars.githubusercontent.com/u/1003724?v=3","gravatar_id":"","url":"https://api.github.com/users/geronime","html_url":"https://github.com/geronime","followers_url":"https://api.github.com/users/geronime/followers","following_url":"https://api.github.com/users/geronime/following{/other_user}","gists_url":"https://api.github.com/users/geronime/gists{/gist_id}","starred_url":"https://api.github.com/users/geronime/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/geronime/subscriptions","organizations_url":"https://api.github.com/users/geronime/orgs","repos_url":"https://api.github.com/users/geronime/repos","events_url":"https://api.github.com/users/geronime/events{/privacy}","received_events_url":"https://api.github.com/users/geronime/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-12-14T15:18:03Z","updated_at":"2015-12-16T17:29:23Z","closed_at":"2015-12-14T15:23:09Z","body":"Simplified code to reproduce the issue:\r\n\r\n```javascript\r\n'use strict';\r\n\r\nvar mongoose = require('mongoose'),\r\n    bluebird = require('bluebird');\r\n\r\nmongoose.Promise = bluebird;\r\n\r\nmongoose.connect('mongodb://localhost/test');\r\n\r\nvar someSchema = mongoose.Schema({someKey: String});\r\nsomeSchema.pre('validate', function (next) {\r\n    this.invalidate('someKey', 'invalid', 'someVal');\r\n    next();\r\n});\r\n\r\nvar SomeModel = mongoose.model('SomeModel', someSchema, 'test');\r\n\r\nmongoose.connection.once('open', function (err) {\r\n    if (err) {\r\n        throw err;\r\n    }\r\n\r\n    SomeModel.create({}, function(err, doc) {\r\n        if (err) {\r\n            console.log('Error: %j', err.message);\r\n        } else {\r\n            console.log('Saved: %j', doc);\r\n        }\r\n        mongoose.connection.close();\r\n    });\r\n});\r\n```\r\n\r\nI get:\r\n```bash\r\n$ node index.js\r\nError: \"SomeModel validation failed\"\r\nUnhandled rejection ValidationError: SomeModel validation failed\r\n    at MongooseError.ValidationError (/private/tmp/mongoose-invalidate-test/node_modules/mongoose/lib/error/validation.js:23:11)\r\n    at model.Document.invalidate (/private/tmp/mongoose-invalidate-test/node_modules/mongoose/lib/document.js:1280:32)\r\n    at model.<anonymous> (/private/tmp/mongoose-invalidate-test/index.js:12:10)\r\n    at model._next (/private/tmp/mongoose-invalidate-test/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:62:30)\r\n    at model.proto.(anonymous function) [as $__original_validate] (/private/tmp/mongoose-invalidate-test/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:108:20)\r\n    at model.Object.defineProperty.value.fn (/private/tmp/mongoose-invalidate-test/node_modules/mongoose/lib/schema.js:229:16)\r\n    at model._next (/private/tmp/mongoose-invalidate-test/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:62:30)\r\n    at model.proto.(anonymous function) [as $__original_save] (/private/tmp/mongoose-invalidate-test/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:108:20)\r\n    at /private/tmp/mongoose-invalidate-test/node_modules/mongoose/lib/model.js:1755:18\r\n    at /private/tmp/mongoose-invalidate-test/node_modules/mongoose/node_modules/async/lib/async.js:570:21\r\n    at /private/tmp/mongoose-invalidate-test/node_modules/mongoose/node_modules/async/lib/async.js:249:17\r\n    at /private/tmp/mongoose-invalidate-test/node_modules/mongoose/node_modules/async/lib/async.js:125:13\r\n    at Array.forEach (native)\r\n    at _each (/private/tmp/mongoose-invalidate-test/node_modules/mongoose/node_modules/async/lib/async.js:46:24)\r\n    at async.each (/private/tmp/mongoose-invalidate-test/node_modules/mongoose/node_modules/async/lib/async.js:124:9)\r\n    at _asyncMap (/private/tmp/mongoose-invalidate-test/node_modules/mongoose/node_modules/async/lib/async.js:248:13)\r\n    at Object.map (/private/tmp/mongoose-invalidate-test/node_modules/mongoose/node_modules/async/lib/async.js:219:23)\r\n    at _parallel (/private/tmp/mongoose-invalidate-test/node_modules/mongoose/node_modules/async/lib/async.js:568:20)\r\n    at Object.async.parallel (/private/tmp/mongoose-invalidate-test/node_modules/mongoose/node_modules/async/lib/async.js:598:9)\r\n    at /private/tmp/mongoose-invalidate-test/node_modules/mongoose/lib/model.js:1762:11\r\n    at Promise._execute (/private/tmp/mongoose-invalidate-test/node_modules/bluebird/js/release/debuggability.js:159:9)\r\n    at Promise._resolveFromExecutor (/private/tmp/mongoose-invalidate-test/node_modules/bluebird/js/release/promise.js:460:18)\r\n```\r\n\r\nCommenting out `mongoose.Promise = bluebird` I get:\r\n```bash\r\n$ node index.js\r\nError: \"SomeModel validation failed\"\r\n```\r\n\r\nI found some issues related to \"Unhandled rejection Error\" warnings but not exactly this one. Is this an error on my side, issue in mongoose or bluebird?\r\n\r\nNode.js v4.2.3, mongoose 4.3.1, bluebird 3.0.6."},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/165184897","html_url":"https://github.com/Automattic/mongoose/issues/3669#issuecomment-165184897","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/3669","id":165184897,"user":{"login":"geronime","id":1003724,"avatar_url":"https://avatars.githubusercontent.com/u/1003724?v=3","gravatar_id":"","url":"https://api.github.com/users/geronime","html_url":"https://github.com/geronime","followers_url":"https://api.github.com/users/geronime/followers","following_url":"https://api.github.com/users/geronime/following{/other_user}","gists_url":"https://api.github.com/users/geronime/gists{/gist_id}","starred_url":"https://api.github.com/users/geronime/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/geronime/subscriptions","organizations_url":"https://api.github.com/users/geronime/orgs","repos_url":"https://api.github.com/users/geronime/repos","events_url":"https://api.github.com/users/geronime/events{/privacy}","received_events_url":"https://api.github.com/users/geronime/received_events","type":"User","site_admin":false},"created_at":"2015-12-16T17:29:23Z","updated_at":"2015-12-16T17:29:23Z","body":"I may have found an issue related to Bluebird and unhandled rejection: In [schema/documentarray](https://github.com/Automattic/mongoose/blob/master/lib/schema/documentarray.js#L100) the validate is done using callback, therefore Bluebird will scream about unhandled rejection."}}