При отправке запроса происходит ошибка "MongoServerError: E11000 duplicate key error collection: prj.users index: email_1 dup key: { email: null }".
Как ее можно решить?
Код:
...
const {Schema, model} = require('mongoose');
const userSchema = new Schema({
name: {
type: String,
required: true
},
surname: {
type: String,
required: true
},
login: {
type: String,
unique: true,
required: true
},
hashedPassword: {
type: String,
required: true
},
role: {
type: String,
required: true
},
classObj: {
type: String
},
books: [{ type: Schema.Types.ObjectId, ref: 'books' }]
});
const User = model('users', userSchema);
app.post('/auth/reg', async (req, res) => {
try {
const { name } = req.body;
const { surname } = req.body;
const { login } = req.body;
const { password } = req.body;
const { role } = req.body;
const { classObj } = req.body || '';
// const { books } = req.body;
const hashedPwd = await bcrypt.hash(password, 8);
const userr = new User({
name: name,
surname: surname,
login: login,
hashedPassword: hashedPwd,
role: role,
classObj: classObj,
books: []
});
await userr.save();
return res.status(200).json({
msg: "Пользователь успешно зарегистрирован"
});
} catch (e) {
console.log(e);
return res.status(500).json({
msg: "Не удалось добавить пользователя",
error: e.message
})
}
});