【发布时间】:2017-03-02 18:55:48
【问题描述】:
我的数据库架构是
var Account = new Schema({
email : String,
name : String ,
family : String ,
password : String
});
我想使用电子邮件和密码进行护照身份验证。我的代码是:
var LocalStrategy = require('passport-local').Strategy;
passport.use('register', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback : true
},
function(req, email, password, done) {
Account.findOne({ 'email' : email }, function(err, user) {
if (err)
{
return done(err);
}
else
{
var newaccount = new Account();
newaccount.password = bcrypt.hashSync(req.body.password);
newaccount.email = req.body.email;
newaccount.name = req.body.name;
newaccount.family = req.body.family;
// save the user
newaccount.save(function(err) {
if (err)
{
console.log('Error in Saving user: '+err);
throw err;
}
}
});
虽然我在
中没有任何用户名字段MongoError: E11000 duplicate key error index: Account.accounts.$username_1 dup key: { : null }
我第一次可以插入数据库并且我没有任何错误但是第二次我有这个错误我认为它假设用户名作为键并且第一次它将用户名设置为空所以第二次它由于重复键,想尝试将用户名设置为空抛出错误。我在互联网上搜索了很多。我删除了数据库并使用了db.Account.dropIndexes(),但问题没有解决。
【问题讨论】:
-
这与此处发布的问题相同。 stackoverflow.com/questions/24430220/…
-
将使用的集合是
accounts,而不是Account。
标签: node.js mongodb express mongoose passport.js