gpt4 book ai didi

javascript - User.findOne() 不是函数

转载 作者:搜寻专家 更新时间:2023-11-01 00:21:30 27 4
gpt4 key购买 nike

passport.use('local-signup', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
(req, email, password, done) => {
// asynchronous
// User.findOne wont fire unless data is sent back
process.nextTick(() => {

// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'email' : email },function(err, user){
// if there are any errors, return the error

if (err)
return done(err);

// check to see if theres already a user with that email
if (user) {
return done(null, false, {'errorMessages': 'That email is already taken.'});
} else {

// if there is no user with that email
// create the user
let newUser = new User();

// set the user's local credentials
newUser.name = req.body.fullname;
//newUser.email = email;
newUser.password = newUser.generateHash(password);


// save the user
newUser.save((err)=>{
if (err)
return done(err);
return done(null, newUser);
});
}
});
});
}));

上面的代码是在node js中使用passport js身份验证的,本地注册的代码不起作用。

在上面的代码中我得到了错误:

User.findOne() 不是函数

我的架构没问题...请帮忙

最佳答案

您需要(如果您还没有)使用类似模型的数据创建实例

var UserDetails = mongoose.model('userInfo', UserDetail);

现在您应该可以在这里使用 .findOne

并确保您在集合中为您的日期定义结构,例如..

 var Schema = mongoose.Schema;
var UserDetail = new Schema({
username: String,
password: String
}, {
collection: 'userInfo'
});

关于javascript - User.findOne() 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41924961/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com