I am trying to make [email protected] the same as [email protected] and here is the User.js file. I am unable to find a way to make the email as well as the username case insensitive. @username should be the same as @UserName. I read about queries here but couldn't figure out how to add it in the schema. I want the request to have any case but it should not differentiate between upper and lower when comparing it with the stored one in the database.
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema(
{
username: {
type: String,
required: true,
min: 3,
max: 15,
unique: true,
index: {
unique: true,
collation: { locale: 'en', strength: 2 },
},
},
email: {
type: String,
required: true,
max: 50,
unique: true,
index: {
unique: true,
collation: { locale: 'en', strength: 2 },
},
},
password: {
type: String,
required: true,
min: 6,
},
profilePicture: {
type: String,
default: '',
},
cover: {
type: String,
default: '',
},
followers: {
type: Array,
default: [],
},
following: {
type: Array,
default: [],
},
isAdmin: {
type: Boolean,
default: false,
},
},
{ timestamps: true }
);
module.exports = mongoose.model('User', UserSchema);
Another thing is, is the following code making any sense?
index: {
unique: true,
collation: { locale: 'en', strength: 2 }
CodePudding user response:
username: {
type: String,
required: true,
min: 3,
max: 15,
lowercase:true,
unique: true,
index: {
unique: true,
collation: { locale: 'en', strength: 2 },
},
}
You can fix the problem by setting "lowercase:true"
When you get the input value,
let str = "[email protected]"; str.toLowerCase(); ("[email protected]")
will be. You can do it this way.