简体   繁体   English

Passport.js .authenticate()抛出TypeError:未定义不是函数

[英]Passport.js .authenticate() throwing TypeError: undefined is not a function

I'm new to passport (and node in general) and have been using this tutorial ( http://mherman.org/blog/2015/01/31/local-authentication-with-passport-and-express-4/#.VczPbTBViko ) to add passport authentication to a larger web app. 我是通行证的新手(并且通常是结点),并且一直在使用本教程( http://mherman.org/blog/2015/01/31/local-authentication-with-passport-and-express-4/# .VczPbTBViko )将护照身份验证添加到较大的Web应用程序。 I think I have been following things correctly, but when I start the app in terminal, I get the following error. 我认为我一直在正确地进行操作,但是在终端中启动应用程序时,出现以下错误。

passport.use(new LocalStrategy(Admin.authenticate()));
                                     ^
TypeError: undefined is not a function
    at Object.<anonymous> (/Users/goldru/design-data/app.js:37:38)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/goldru/design-data/bin/www:7:11)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

I checked package.json and all of the relevant libraries appear to be installed. 我检查了package.json,所有相关的库似乎都已安装。

Here is the relevant code. 这是相关的代码。

from app.js: 来自app.js:

//connect to db; require models
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/design-data-test');
var Test = require('./models/Tests');
var Question = require('./models/Question');
var User = require('./models/User');
var Option = require('./models/Option');
var Admin = require('./models/Admin');

//require passport
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;

var routes = require('./routes/index');

var express = require('express');
var app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

//passport setup
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);

//config admin
var Admin = require('./models/Admin');
passport.use(new LocalStrategy(Admin.authenticate()));
passport.serializeUser(Admin.serializeUser());
passport.deserializeUser(Admin.deserializeUser());

admin.js: admin.js:

var mongoose = require('mongoose');
var passportLocalMongoose = require('passport-local-mongoose')

var AdminSchema = new mongoose.Schema({
    username: String,
    password: String
});

AdminSchema.plugin(passportLocalMongoose);

module.export = mongoose.model('Admin', AdminSchema);

It's a typo. 这是一个错字。 You have to use module.exports and not module.export in admin.js. 你必须使用module.exports而不是module.export在admin.js。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM