I want my users to have a phone or an email (at least one of them) that's why I used these options for phone and email in my schema:
const schema = new Schema({
phone: { type: String, unique: true, required: false },
email: { type: String, unique: true, required: false },
passwordHash: { type: String, required: true },
role: { type: String, required: true },
verificationToken: String
});
And this is the code to save a new doc to the collection:
async function register(params, origin) {
// create account object
console.log('params', params);
const account = new db.Account(params);
// first registered account is an admin
const isFirstAccount = (await db.Account.countDocuments({})) === 0;
account.role = isFirstAccount ? Role.Admin : Role.User;
account.verificationToken = randomTokenString();
// hash password
account.passwordHash = hash(params.password);
console.log('account reached 1');
// save account
try {
await account.save();
} catch(err) {
console.log(err) // this gets the error
}
console.log('account reached 2');
console.log('account reached 3');
return { success: true, msg: 'Registration successful' }
}
I sent two different types of params
to the register function one with phone property and one with email. when I use the kind of params
with email property everything works fine and I save a new user to the collection this is the console here:
But when I use phone instead of the email I get this error and nothing is saved into the collection:
How can I fix this error? I'm new to MongoDB.
Edit 1
I used this and the same error happens (this is the whole schema):
const schema = new Schema({
phone: { type: String, required: false },
email: { type: String, required: false },
passwordHash: { type: String, required: true },
title: { type: String, required: false },
firstName: { type: String, required: false },
lastName: { type: String, required: false },
acceptTerms: Boolean,
role: { type: String, required: true },
verificationToken: String,
verified: Date,
resetToken: {
token: String,
expires: Date
},
passwordReset: Date,
created: { type: Date, default: Date.now },
updated: Date,
userLevel: { type: String, default: 'starter' },
cards: {
starter : [unitSchema],
intermediate : [unitSchema],
advanced : [unitSchema],
}
});
schema.index({'phone': 1, 'email': 1}, {unique: true});
Edit 2
This is not working too:
const schema = new Schema({
phone: { type: String, unique: true, required: false, sparse: true },
email: { type: String, unique: true, required: false, sparse: true },
...
Edit 3
Used this but it's not working (something has changed or this is not the solution at all!!)
const schema = new Schema({
phone: {
type: String,
trim: true,
index: {
unique: true,
partialFilterExpression: {phone: {$type: "string"}}
}
},
email: {
type: String,
trim: true,
index: {
unique: true,
partialFilterExpression: {email: {$type: "string"}}
}
},
// phone: { type: String, unique: true, required: false, sparse: true },
// email: { type: String, unique: true, required: false, sparse: true },
...