My heroku app crashes when passport gives the error "a user with the given username is already registered"

hi everyone
i’m currently working on the voting app. but i’m having a problem
when i try to register with a pre-registered username and passport gives the error “a user with the given username is already registered”

my heroku app crashes , and it doesn’t restart . even though i tried “heroku ps:scale web=1”
here’s the code of my express/mongodb server:: https://github.com/BahaaZidan/ex40/tree/master/server

and here’s the heroku log .
any help is appreciated. thanks in advance.

router.post('/register', (req, res) => {
  users.register(new users({ username : req.body.username }), req.body.password, (err, val) => {
    if (err) return res.status(500).json({status: `Authentication Problem :: ${err}`});
    val.save((err,user) => {
      if (err) return res.status(500).json({status: `Authentication Problem :: ${err}`});
      passport.authenticate('local')(req, res, () => { 
        return res.status(200).json({status: 'Registration Successful!'});
      });
    });
  })
});

this solved the problem.
turns out i wasn’t handling the exception i was just throwing it .

1 Like