How to handle Error: ER_DUP_ENTRY: Duplicate entry in nodejs

15,496

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);
    }
}
Share:
15,496
ketan pradhan
Author by

ketan pradhan

Updated on June 22, 2022

Comments

  • ketan pradhan
    ketan pradhan almost 2 years

    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