{"action":"created","issue":{"url":"https://api.github.com/repos/MasteringMEAN/MEAN-Web-Development/issues/2","labels_url":"https://api.github.com/repos/MasteringMEAN/MEAN-Web-Development/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/MasteringMEAN/MEAN-Web-Development/issues/2/comments","events_url":"https://api.github.com/repos/MasteringMEAN/MEAN-Web-Development/issues/2/events","html_url":"https://github.com/MasteringMEAN/MEAN-Web-Development/issues/2","id":92772801,"number":2,"title":"Problems with saveOAuthUserProfile function","user":{"login":"isimmons","id":1780886,"avatar_url":"https://avatars.githubusercontent.com/u/1780886?v=3","gravatar_id":"","url":"https://api.github.com/users/isimmons","html_url":"https://github.com/isimmons","followers_url":"https://api.github.com/users/isimmons/followers","following_url":"https://api.github.com/users/isimmons/following{/other_user}","gists_url":"https://api.github.com/users/isimmons/gists{/gist_id}","starred_url":"https://api.github.com/users/isimmons/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/isimmons/subscriptions","organizations_url":"https://api.github.com/users/isimmons/orgs","repos_url":"https://api.github.com/users/isimmons/repos","events_url":"https://api.github.com/users/isimmons/events{/privacy}","received_events_url":"https://api.github.com/users/isimmons/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-07-03T01:44:51Z","updated_at":"2015-07-05T02:51:45Z","closed_at":null,"body":"Code here https://github.com/MasteringMEAN/MEAN-Web-Development/blob/master/6-Managing-Authentication-with-Passport/app/controllers/users.server.controller.js is not the same as code here\r\nhttps://masteringmean.com/lessons/198-Managing-User-Authentication-Using-Passport-Understanding-OAuth\r\n\r\nUsing code masteringmean.com  I get  \"_this is not defined\" and \"undefined is not a function\" for res.redirect('/signup');\r\n\r\nMarked them in the code below.\r\n\r\n```\r\nexports.saveOAuthUserProfile = function(req, profile, done) {\r\n\r\n  User.findOne({\r\n    provider: profile.provider,\r\n    providerId: profile.providerId\r\n  }, function(err, user) {\r\n    if (err) {\r\n      return done(err);\r\n    } else {\r\n      if (!user) {\r\n        var possibleUsername = profile.username || ((profile.email) ? profile.email.split('@')[0] : '');\r\n\r\n        User.findUniqueUsername(possibleUsername, null, function(availableUsername) {\r\n          profile.username = availableUsername;\r\n\r\n          user = new User(profile);\r\n\r\n          user.save(function(err) {\r\n            if (err) {\r\n              var message = _this.getErrorMessage(err); /** remove _this seems to fix it **/\r\n\r\n              req.flash('error', message);\r\n              return res.redirect('/signup'); /** undefined is not a function **/\r\n            }\r\n\r\n            return done(err, user);\r\n          });\r\n        });\r\n      } else {\r\n        return done(err, user);\r\n      }\r\n    }\r\n  });\r\n};\r\n```\r\nNote: this problem didn't happen when implementing the facebook and twitter oauth parts of the tutorial so it's odd this is happening now. Appreciate any help."},"comment":{"url":"https://api.github.com/repos/MasteringMEAN/MEAN-Web-Development/issues/comments/118572271","html_url":"https://github.com/MasteringMEAN/MEAN-Web-Development/issues/2#issuecomment-118572271","issue_url":"https://api.github.com/repos/MasteringMEAN/MEAN-Web-Development/issues/2","id":118572271,"user":{"login":"isimmons","id":1780886,"avatar_url":"https://avatars.githubusercontent.com/u/1780886?v=3","gravatar_id":"","url":"https://api.github.com/users/isimmons","html_url":"https://github.com/isimmons","followers_url":"https://api.github.com/users/isimmons/followers","following_url":"https://api.github.com/users/isimmons/following{/other_user}","gists_url":"https://api.github.com/users/isimmons/gists{/gist_id}","starred_url":"https://api.github.com/users/isimmons/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/isimmons/subscriptions","organizations_url":"https://api.github.com/users/isimmons/orgs","repos_url":"https://api.github.com/users/isimmons/repos","events_url":"https://api.github.com/users/isimmons/events{/privacy}","received_events_url":"https://api.github.com/users/isimmons/received_events","type":"User","site_admin":false},"created_at":"2015-07-05T02:51:45Z","updated_at":"2015-07-05T02:51:45Z","body":"Problem solved thanks to this SO article. http://stackoverflow.com/questions/31223425/mongodb-dup-key-error-11000-crashes-node-server\r\n\r\nTurns out it was because res was undefined just like the original error was telling me.\r\n\r\nStill _this.getErrorMessage() is wrong\r\n\r\nSince we used the option in our strategies to pass the req to the callback but there is no way to pass res along, thankfully express attaches res to req so we can use it that way\r\n\r\nThe corrected user.save function inside saveOauthUserProfile:\r\n```\r\nuser.save(function(err) {\r\n            if (err) {\r\n              var message = getErrorMessage(err);\r\n              req.flash('error', message);\r\n              return req.res.redirect('/signup'); //notice the use of req.res\r\n            }\r\n```"}}