node.js - Nodejs - Passport ,完成不是一个函数

标签 node.js express passport.js

我正在学习 Passport 和 Passport 本地,使用这个版本:

"passport": "^0.4.0", "passport-local": "^1.0.0"

问题是,当尝试在获取表单数据后写入用户注册表时,收到此错误消息:

(node:9120) UnhandledPromiseRejectionWarning: TypeError: done is not a function at Strategy.passport.use.LocalStrategy [as _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)
}))

最佳答案

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

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

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)
}))

关于node.js - Nodejs - Passport ,完成不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53898198/

相关文章:

Javascript - 将 this 参数绑定(bind)到 Promise

node.js - 从sqlite3获取BIGINT值

arrays - Nodejs S3删除多个对象错误

Relay 和 GraphQL 中的身份验证

javascript - Node js Next 不是 Passport 策略错误中的函数

javascript - 二进制操作参数类型字符串与类型字符串不兼容

node.js - 在对话框流中使用对象列表作为参数设置上下文

在 express js 中测试 View

javascript - 对象中的NodeJs FindbyId

node.js - Passport.js 在 Firefox 上不持久