{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/5639","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/5639/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/5639/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/5639/events","html_url":"https://github.com/Automattic/mongoose/issues/5639","id":257006223,"number":5639,"title":"findOne throws Internal Server Error if object isn't found","user":{"login":"martijnhiemstra","id":22905318,"avatar_url":"https://avatars1.githubusercontent.com/u/22905318?v=4","gravatar_id":"","url":"https://api.github.com/users/martijnhiemstra","html_url":"https://github.com/martijnhiemstra","followers_url":"https://api.github.com/users/martijnhiemstra/followers","following_url":"https://api.github.com/users/martijnhiemstra/following{/other_user}","gists_url":"https://api.github.com/users/martijnhiemstra/gists{/gist_id}","starred_url":"https://api.github.com/users/martijnhiemstra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/martijnhiemstra/subscriptions","organizations_url":"https://api.github.com/users/martijnhiemstra/orgs","repos_url":"https://api.github.com/users/martijnhiemstra/repos","events_url":"https://api.github.com/users/martijnhiemstra/events{/privacy}","received_events_url":"https://api.github.com/users/martijnhiemstra/received_events","type":"User","site_admin":false},"labels":[{"id":335758689,"url":"https://api.github.com/repos/Automattic/mongoose/labels/needs%20clarification","name":"needs clarification","color":"EB219B","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2017-09-12T11:15:43Z","updated_at":"2017-09-26T00:39:57Z","closed_at":"2017-09-26T00:39:57Z","author_association":"NONE","body":"When I use the findOne or findOneAndUpdate method, if the filtering doesn't find an object then a 500 Internal Server Error is thrown which is wrong if you ask me. A 500 error would suggest that an exception has happen and a non existing id isn't an exception, it's a possibility. Exceptions are  This is my code:\r\n\r\n    MyObject.findOne({\r\n        _id: mongoose.Types.ObjectId(req.params.id)\r\n      }, function(err, myObject) {\r\n         // This function isn't called.\r\n      }\r\n\r\nWhat should happen is that the err property should be supplied and that the function is called so that we have control over how to handle a non existing id.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/332052433","html_url":"https://github.com/Automattic/mongoose/issues/5639#issuecomment-332052433","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/5639","id":332052433,"user":{"login":"vkarpov15","id":1620265,"avatar_url":"https://avatars2.githubusercontent.com/u/1620265?v=4","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":"2017-09-26T00:39:57Z","updated_at":"2017-09-26T00:39:57Z","author_association":"COLLABORATOR","body":"Yeah in mongoose and MongoDB in general `findOne()` that doesn't match any document is not considered an error. You can easily write middleware to make `findOne()` error if no docs were found though:\r\n\r\n```javascript\r\nschema.post('findOne', function(result, next) {\r\n  if (result == null) {\r\n    return next(new Error('Can\\'t find object'));\r\n  }\r\n  next();\r\n});\r\n```"}}
{"actor":{"display_login":"vkarpov15"}}