Handle multiple duplicate key error in mongoose (MongoDB) #13399
Unanswered
lucianopinochet
asked this question in
Q&A
Replies: 1 comment
-
Unfortunately not. The duplicate key error comes from the MongoDB server, and the MongoDB server only reports the first duplicate key error it finds on a given document. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to get from document.save() all the duplicate key errors, the schema declares a username and email unique, at the moment of handling duplicate key errors, i just get the first one, not the two of them.
const UserSchema = new mongoose.Schema<IUser>( { userName:{ type: String, required: true, max:20, unique: true, }, email:{ type: String, required: true, max: 50, unique: true, }, },{timestamps:true} )
and here is where the error occurs :
const newUser = new User({ userName email, }) const savedUser = await newUser.save()
but, just the first duplicate error gets caught,
is there a way in which, save return both errors and not just the first caught.
Beta Was this translation helpful? Give feedback.
All reactions