Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Error: ER_DUP_ENTRY: Duplicate entry in nodejs

I have unique column in mysql database when and duplicate entry occur's it gives Error: ER_DUP_ENTRY: Duplicate entry how to handle the error form nodejs

like image 348
ketan pradhan Avatar asked Mar 13 '17 14:03

ketan pradhan


1 Answers

Put the save onto try block and catch all errors. add a specific check for duplicates and handle the rest as defaults

try {
    return await this.userProfilesRepository.save(userProfile);
} catch (err) {
   if (err.code === 'ER_DUP_ENTRY') {
       //handleHttpErrors(SYSTEM_ERRORS.USER_ALREADY_EXISTS);
   } else {
       //handleHttpErrors(err.message);
    }
}
like image 182
Sizwe Ntanzi Avatar answered Sep 16 '22 19:09

Sizwe Ntanzi