Node.js 错误:bcrypt需要数据和散列参数。MongoDB设置不正确
我正试图用Express和MongoDB设置Passport。目前,我可以在数据库中注册用户。但每当我尝试登录时,我都会收到一个错误,说明需要数据和散列参数。现在我的Server.js文件如下Node.js 错误:bcrypt需要数据和散列参数。MongoDB设置不正确,node.js,mongodb,passport.js,bcrypt,Node.js,Mongodb,Passport.js,Bcrypt,我正试图用Express和MongoDB设置Passport。目前,我可以在数据库中注册用户。但每当我尝试登录时,我都会收到一个错误,说明需要数据和散列参数。现在我的Server.js文件如下 const mongoose = require('mongoose'); const User = require('./models/users') const initializePassport = require('./passport-config') initiali
const mongoose = require('mongoose');
const User = require('./models/users')
const initializePassport = require('./passport-config')
initializePassport(
passport,
email => User.find({email: email}),
id => User.find({id: id})
)
app.post('/register', checkNotAuthenticated, async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10)
const newUser = new User({
id: Date.now().toString(),
name: req.body.name,
email: req.body.email,
password: hashedPassword
})
res.redirect('/login')
console.log(newUser)
} catch {
res.redirect('/register')
}
And my Passport-Config.js file like this `
const LocalStrategy = require('passport-local').Strategy
const bcrypt = require('bcrypt');
const User = require('./models/users')
function initialize(passport, getUserByEmail, getUserById) {
const authenticateUser = async (email, password, done) => {
const user = getUserByEmail(email)
if (user === null) {
return done(null, false, { message: 'No user with that email' })
}
try {
if (await bcrypt.compare(password, user.password)) {
return done(null, user)
} else {
return done(null, false, { message: 'Password incorrect' })
}
} catch (e) {
return done(e)
}
}
passport.use(new LocalStrategy({ usernameField: 'email' }, authenticateUser))
passport.serializeUser((user, done) => done(null, user.id))
passport.deserializeUser((id, done) => {
return done(null, User.findById({user: id}))
})
}
`
我已经使用console.log()语句进行了一些调查(并不自豪),但我想我已经找到了问题所在。如果我们在此处添加第一个控制台日志语句:
app.post('/register', checkNotAuthenticated, async (req, res) => {
try {
console.log("BCRYPT COMPARE RUNS HERE")
const hashedPassword = await bcrypt.hash(req.body.password, 10)
const newUser = new User({
id: Date.now().toString(),
name: req.body.name,
email: req.body.email,
password: hashedPassword
})
res.redirect('/login')
console.log(newUser)
} catch {
res.redirect('/register')
}
第二个是:
const initializePassport = require('./passport-config')
initializePassport(
passport,
email => User.find({email: email}).then((result) => { console.log("USER DATA EXTRACTED HERE") }).catch((err) => { console.log(err) }),
id => User.find({id: id})
)
下次单击login时,您应该会看到如下输出:
Listening on port 3000
BCRYPT COMPARE HAPPENING
Error: data and hash arguments required
...
...
...
USER DATA EXTRACTED HERE
请注意,bcrypt.compare是在我们实际能够从数据库中获取用户信息之前运行的?这意味着该函数中的所有参数都为null,这就是返回该错误的原因。现在,我不是JS专家,但这可以通过添加一条等待语句来解决:
function initialize(passport, getUserByEmail, getUserById) {
const authenticateUser = async (email, password, done) => {
const user = await getUserByEmail(email)
if (user === null) {
return done(null, false, { message: 'No user with that email' })
}
这确保在脚本中移动之前从DB查询用户信息。Ahh,在尝试将本地JSON对象上的用户转换为mongodb集合时,我也面临相同的错误。。。我认为这与bcrypt比较方法有关,但不太确定。很高兴知道有人和我面临同样的问题!