This is the user snippet
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
name: { type: String, required: true },
lastName: { type: String, required: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
userType: { type: String, required: true },
workplaces: { type: [Number], required: true },
});
const User = mongoose.model('User', userSchema, 'users');
module.exports = User;
This is the code that i have
const accountExists = await User.findOne({ email: extractData.email})
console.log(accountExists);
If the email that the user has entered exists in the db there should be an error but the findOne always return null even if there is a user with the same email
2
Answers
Error while creating a new user
It says, the error is coming while creating new user.Because there is already a user with same identity.
based on the error it means that the user isn’t saved , because that user is already stored in the database. so in your insert controller try to make a condition and first try to extract properties from object that you send to you database:
for exemple :
{username , email ,password } = req.body;
and use this approach instead:
//check if we have the same userEmail that the user entered in our database:
const userEmail = await User.findOne({ email });