Catch/Handle MySQL Duplicate Entry Error - with NodeJS, PassportJS, Express, connect-flash, Heroku Catch/Handle MySQL Duplicate Entry Error - with NodeJS, PassportJS, Express, connect-flash, Heroku heroku heroku

Catch/Handle MySQL Duplicate Entry Error - with NodeJS, PassportJS, Express, connect-flash, Heroku


maybe its 3 years late but I'm leaving the answer, better late than never!.

   if(err){  //we make sure theres an error (error obj)        if(err.errno==1062){           req.flash('message','The entry already exist.'); //we send the flash msg        return res.redirect('/admin/userPanel');        db.end();        }        else{            throw err;        db.end();        }}

errorno is the property that you are looking for, I got it when I printed err to console using console.log(err).

Here you can check all the errors number https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html

it was a pleasure! Good luck to the next good soul that read this.


pretty straight and clear solution://----------------

connection.query('write your mysql query', function(err,rows){      if(err)      {        if(err.code == 'ER_DUP_ENTRY' || err.errno == 1062)        {            console.log('Here you can handle duplication')        }        else{           console.log('Other error in the query')        }      }else{         console.log('No error in the query')      } })