Nodejs-Passport,完成不是功能

时间:2018-12-22 18:16:18

标签: node.js express passport.js

我正在使用以下版本来学习护照和本地护照:

  

“ passport”:“ ^ 0.4.0”,“ passport-local”:“ ^ 1.0.0”

问题在于,当获取表单数据后尝试编写用户注册表时,会收到以下错误消息:

  

(节点:9120)UnhandledPromiseRejectionWarning:TypeError:done不是   功能       在Strategy.passport.use.LocalStrategy上[作为_verify](C:\ codes \ Gclimb \ passport \ localAuth.js:27:5)

这是我的localAuth.js的代码

const passport = require('passport')
const LocalStrategy = require('passport-local').Strategy
const User = require('../models/user')

passport.serializeUser((user, done) => {
    done(null, user.id)
})

passport.deserializeUser(async(id, done) => {
    const user = await User.findById(id)
    done(null, user)
})

passport.use('local-signup', new LocalStrategy({

    usernameField: 'username',
    passwordField: 'password',
    passReqToCallback: true

}, async (req, username, email, password, done) => {

    const user = new User()
    user.email = email
    user.password = password
    user.username = username
    await user.save()
    done(null, user) // <----- Line 27(error)
}))

2 个答案:

答案 0 :(得分:2)

您传递的参数少于您的期望

我暂时删除了该电子邮件,如果可行,我们将担心该电子邮件

const passport = require('passport')
const LocalStrategy = require('passport-local').Strategy
const User = require('../models/user')

passport.serializeUser((user, done) => {
    done(null, user.id)
})

passport.deserializeUser(async(id, done) => {
    const user = await User.findById(id)
    done(null, user)
})

passport.use('local-signup', new LocalStrategy({

    usernameField: 'username',
    passwordField: 'password',
    passReqToCallback: true

}, async (req, username, password, done) => {

    const user = new User()
    user.password = password
    user.username = username
    await user.save()
    done(null, user) // <----- Line 27(error)
}))

答案 1 :(得分:1)

异步删除“ req”。完成后将再次工作。 我认为新版PassportJS中的问题