尝试使用mongodb创建新用户时出现e11000重复密钥错误
发布时间:2022-04-16 07:56:49 317
相关标签:
当我试图创建一个用户E11000重复密钥错误集合:rajTech时,我遇到了这个错误。用户索引:isAdmin_1 dup密钥:{isAdmin:false}
我的用户模式是
import mongoose from 'mongoose'
const userSchema = new mongoose.Schema(
{
name: {type: String, required: true},
email: {type: String, required: true, unique: true},
password: {type: String, required: true},
isAdmin: {type: Boolean, default: false, required: true, unique: true},
},
{
timestamps:true
}
)
const User = mongoose.model('User', userSchema)
export default User
这是我的职责
userRouter.post(
'/signup',
expressAsyncHandler(async (req, res) => {
const newUser = new User({
name: req.body.name,
email: req.body.email,
password: bcrypt.hashSync(req.body.password)
})
const user = await newUser.save()
res.send({
_id: user._id,
name: user.name,
email: user.email,
isAdmin: false,
token: generateToken(user)
})
}))
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报