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);
});
}
});
});
}));
以上代码位于使用护照js身份验证的节点js中,并且本地注册代码不起作用。
在上面的代码中,我得到了错误:
User.findOne() is not a function
。
我的模式没问题..。请帮帮忙
发布于 2017-01-29 19:38:51
您需要(如果您还没有)使用类似的model
创建数据实例
var UserDetails = mongoose.model('userInfo', UserDetail);
现在您应该可以在这里使用.findOne
了。
并确保您在集合中为您的约会对象定义了结构,如..。
var Schema = mongoose.Schema;
var UserDetail = new Schema({
username: String,
password: String
}, {
collection: 'userInfo'
});
发布于 2019-04-01 14:48:41
请使用下面的代码
module.exports = User = mongoose.model('user', UserSchema)
用户应该是模型名,并记住在顶部定义const UserSchema = new Schema
,以便在MongoDB和
用户应该选择您拥有
router.post('/user') (req, res) => { code here }
这样,您就可以将mongoose模式导出到路由用户,这使findOne
可以被看作是mongoose函数。
发布于 2019-02-07 06:27:14
可能您没有从用户模型文件夹导出模型。例: module.exports =mongoose.model(“用户”,UserSchema);
https://stackoverflow.com/questions/41924961
复制相似问题