【发布时间】:2016-10-23 10:13:54
【问题描述】:
var UserSchema = new Schema({
"username": {type: String, unique: true },
"password": String,
})
所以我将属性名称更改为用户名,从那时起事情就变得糟糕了。
无论我做什么,我都会不断收到错误
E11000 重复键错误索引:test.users.$name_1 dup key: { : null }
即使我尝试删除数据库、更改要连接的数据库的名称并从架构中删除 unique: true 时,我仍然会收到此错误。
无论我做什么,我都无法删除这个错误,为什么即使在我删除每个数据库并删除唯一属性之后它仍然抛出这个错误......
这里是保存功能 -
router.post('/register', function(req, res){
var user = new User();
user.username = req.body.username;
//hash the password
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(req.body.password, salt, function(err, hash) {
user.password = hash;
user.save(function(err){
if(err){
console.log(err.message);
if(err.message === "E11000 duplicate key error index: test.users.$name_1 dup key: { : \"" + user.username + "\" }"){ //this was working fine before i changed user.name to user.username
res.json({
error: "name already taken"
});
}else{
res.json({
error: "There was an error processing your registration."
});
}
return(err);
}
res.json({user: user});
})
});
});
});
【问题讨论】: