lal*_*hav 2 javascript node.js
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);
});
}
});
});
}));
Run Code Online (Sandbox Code Playgroud)
上面的代码在使用护照js身份验证的节点js中,本地注册代码不起作用。
在上面的代码中,我得到了错误:
User.findOne() is not a function。
我的架构还可以...请帮助
您需要(如果您尚未创建)使用model like 创建数据实例
var UserDetails = mongoose.model('userInfo', UserDetail);
Run Code Online (Sandbox Code Playgroud)
现在您应该可以.findOne在这里使用了。
并确保您在类似集合之类的日期中定义结构。
var Schema = mongoose.Schema;
var UserDetail = new Schema({
username: String,
password: String
}, {
collection: 'userInfo'
});
Run Code Online (Sandbox Code Playgroud)
小智 5
请使用下面的代码
module.exports = User = mongoose.model('user', UserSchema)
Run Code Online (Sandbox Code Playgroud)
UserSchema = new Schema User 应该是模型名称,并记住在顶部定义 const以在 MongoDB 中创建新模型并
用户应该知道你有的路线
router.post('/user') (req, res) => { code here }
Run Code Online (Sandbox Code Playgroud)
这样,您就可以将 mongoose 模式导出到路由用户,这可以findOne被视为 mongoose 函数。
| 归档时间: |
|
| 查看次数: |
12429 次 |
| 最近记录: |