Skip to content

Instantly share code, notes, and snippets.

@leobrines
Created May 18, 2019 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leobrines/d04ca2ae2a164f32f1e62d5ad05f2b8c to your computer and use it in GitHub Desktop.
Save leobrines/d04ca2ae2a164f32f1e62d5ad05f2b8c to your computer and use it in GitHub Desktop.
Get key from "DuplicateKeyError" MongoDB - NodeJS
try {
await db.collection('users').create({
name,
email,
phone,
password
})
} catch (error) {
if (error.code == 11000) {
const duplicated = error.match(/index:\s([a-z]+)/i);
console.log('The ' + duplicated + ' is already used ');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment