{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/3210","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/3210/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/3210/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/3210/events","html_url":"https://github.com/Automattic/mongoose/issues/3210","id":97806746,"number":3210,"title":"Weird error on occasion","user":{"login":"ORESoftware","id":11139560,"avatar_url":"https://avatars.githubusercontent.com/u/11139560?v=3","gravatar_id":"","url":"https://api.github.com/users/ORESoftware","html_url":"https://github.com/ORESoftware","followers_url":"https://api.github.com/users/ORESoftware/followers","following_url":"https://api.github.com/users/ORESoftware/following{/other_user}","gists_url":"https://api.github.com/users/ORESoftware/gists{/gist_id}","starred_url":"https://api.github.com/users/ORESoftware/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ORESoftware/subscriptions","organizations_url":"https://api.github.com/users/ORESoftware/orgs","repos_url":"https://api.github.com/users/ORESoftware/repos","events_url":"https://api.github.com/users/ORESoftware/events{/privacy}","received_events_url":"https://api.github.com/users/ORESoftware/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-07-28T21:35:09Z","updated_at":"2015-07-29T20:16:26Z","closed_at":null,"body":"My client gets this error message, it has KAREEM and Mongoose labeling, it arises out of nowhere somewhere in the middleware, between router.param and router.post below:\r\n\r\n![screenshot 2015-07-28 14 32 49](https://cloud.githubusercontent.com/assets/11139560/8944481/bbea24d4-3535-11e5-9c1d-bc96e13faf6f.png)\r\n\r\nthis response occurs right after the ```next()``` call at the bottom of the router.param callback\r\n\r\n```javascript\r\nrouter.param('user_id', function (req, res, next, user_id) {\r\n    // typically we might sanity check that user_id is of the right format\r\n    if (user_id == undefined || user_id == null) {\r\n        console.log('null user_id');\r\n        return next(new Error(\"user_id is null\"));\r\n    }\r\n\r\n    var UserModel = req.site.models.User;\r\n    req.specialParams = {};\r\n\r\n    UserModel.get(function (err, User) {\r\n        User.findById(user_id, function (err, user) {\r\n            if (err) {\r\n                return next(err);\r\n            }\r\n            if (!user) {\r\n                req.specialParams.user_model = null;\r\n            }\r\n            else {\r\n                req.specialParams.user_model = user;\r\n            }\r\n\r\n            next();\r\n        });\r\n    });\r\n});\r\n\r\nrouter.post('/:user_id', function (req, res, next) {\r\n\r\n    if(req.specialParams.user_model == null){\r\n        console.log('updating password for a user thats null...');\r\n        return res.json({error:'errrror1'});\r\n    }\r\n    else if(!(req.specialParams.user_model._doc._id.equals(req.user._doc._id))){\r\n        console.log('updating password for a user thats not logged in...');\r\n        return res.json({error:'errrror2'});\r\n    }\r\n    else{\r\n\r\n        var userData = req.body;\r\n        var firstName = userData.firstName;\r\n        var lastName = userData.lastName;\r\n        var username = userData.username;\r\n        var password = userData.current_password;\r\n        var new_password = userData.new_password;\r\n\r\n        var UserModel = req.site.models.User;\r\n        UserModel.get(function(err,User){\r\n\r\n            User.findOne({\r\n                username: username\r\n            }, function (err, user) {\r\n                if (err) {\r\n                    return done(err);\r\n                }\r\n                else if (!user) {\r\n                    return res.json({error:'incorrect username'});\r\n                }\r\n                else{\r\n                    user.validatePassword(password, function (err, isValid) {\r\n                        if (err) {\r\n                            return res.json({error:err});\r\n                        }\r\n                        else if (!isValid) {\r\n                            return res.json({error:'incorrect password'});\r\n                        }\r\n                        else {\r\n\r\n                            return update(User,user,userData,req,res,next);\r\n                        }\r\n\r\n                    });\r\n                }\r\n            });\r\n\r\n            function update(User,user,userData,req,res,next){\r\n\r\n\r\n                bcrypt.hash(new_password, SALT_WORK_FACTOR, function (err, hash) {\r\n                    if (err) {\r\n                        return next(err);\r\n                    }\r\n                    else if (hash == null) {\r\n                        return next(new Error('null/undefined hash'));\r\n                    }\r\n                    else {\r\n                        //user.passwordHash = hash;\r\n                        User.update({ _id: user._id }, { $set: { passwordHash: hash, old_password:userData.passwordHash, password:null,new_password:null }}, function(err,user){\r\n\r\n                            if(err){\r\n                                return res.json({error:'error3'});\r\n                            }\r\n                            else{\r\n                                return res.json({success:user});\r\n                            }\r\n\r\n                        });\r\n                    }\r\n                });\r\n\r\n\r\n\r\n            }\r\n\r\n        });\r\n    }\r\n```"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/126083634","html_url":"https://github.com/Automattic/mongoose/issues/3210#issuecomment-126083634","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/3210","id":126083634,"user":{"login":"ORESoftware","id":11139560,"avatar_url":"https://avatars.githubusercontent.com/u/11139560?v=3","gravatar_id":"","url":"https://api.github.com/users/ORESoftware","html_url":"https://github.com/ORESoftware","followers_url":"https://api.github.com/users/ORESoftware/followers","following_url":"https://api.github.com/users/ORESoftware/following{/other_user}","gists_url":"https://api.github.com/users/ORESoftware/gists{/gist_id}","starred_url":"https://api.github.com/users/ORESoftware/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ORESoftware/subscriptions","organizations_url":"https://api.github.com/users/ORESoftware/orgs","repos_url":"https://api.github.com/users/ORESoftware/repos","events_url":"https://api.github.com/users/ORESoftware/events{/privacy}","received_events_url":"https://api.github.com/users/ORESoftware/received_events","type":"User","site_admin":false},"created_at":"2015-07-29T20:16:25Z","updated_at":"2015-07-29T20:16:25Z","body":"it's unpredictable, possibly due to some race condition, I can't replicate it easily, but if it happens again I will capture the stack trace"}}