Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique email (with unique username) #319

Open
cd4d opened this issue Mar 27, 2021 · 2 comments
Open

Unique email (with unique username) #319

cd4d opened this issue Mar 27, 2021 · 2 comments

Comments

@cd4d
Copy link

cd4d commented Mar 27, 2021

Is it possible to enforce unique email and username at the same time? All I see in the doc is "usernameUnique" option.

In my project that option works and I get an error when I try to register a duplicate username, however it's still possible to register a user with the same email.

@rajan-31
Copy link

As per my knowledge, no you can't do that.

But, what you can do is set that in you model like this,

email: {
        type: String,
        unique: true
}

*Extra info:

  • if "email" is not unique it will throw "duplicate key error"

  • if you want to handle that error specifically, use this

if (err.name === 'MongoError' && err.code === 11000) {
        // handle error
 }

@cd4d
Copy link
Author

cd4d commented Apr 11, 2021

That's what I ended up using. I don't know why it's not implemented, as it's not unusual to enforce both unique values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants