Created
May 18, 2019 21:00
-
-
Save leobrines/d04ca2ae2a164f32f1e62d5ad05f2b8c to your computer and use it in GitHub Desktop.
Get key from "DuplicateKeyError" MongoDB - NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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