护照抛出“未定义不是函数”

Posted

技术标签:

【中文标题】护照抛出“未定义不是函数”【英文标题】:Passport throwing "undefined is not a function" 【发布时间】:2014-07-14 02:27:04 【问题描述】:

这是我第一次使用护照。

当我尝试启动我的应用程序时,我不断收到此错误:

passport.use(new LocalStrategy(Account.authenticate()));
             ^
TypeError: undefined is not a function
    at Object.<anonymous> (/Users/drewwyatt/Sites/JS/Node/MWInternal/app.js:38:14)

这是 app.js 的(相关)部分

var express = require('express');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var mongoose = require('mongoose');
var passport = require('passport');
var LocalStrategy = require('passport-local').LocalStrategy;

var app = express();
var connectionString = '***************';


// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('less-middleware')(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) 
  app.use(express.errorHandler());


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

mongoose.connect(connectionString);

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

...

/models/account.js

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

var Account = new Schema(
    email: String,
    password: String
);

Account.plugin(passportLocalMongoose);

module.exports = mongoose.model('Account', Account);

我错过了什么?

【问题讨论】:

【参考方案1】:

改变

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

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

passport-local 模块 exports Strategy:

var Strategy = require('./strategy');
exports.Strategy = Strategy;

【讨论】:

刚刚在passport-local v1.0.0中找到源代码是:module.exports = Strategy;,不知道是不是变了。

以上是关于护照抛出“未定义不是函数”的主要内容,如果未能解决你的问题,请参考以下文章

未捕获的类型错误未定义不是函数

SCRIPT5009:“JSON”在 IE 10 中未定义属性“$”的值为 null 或未定义,不是函数对象

TypeError:未定义'不是函数(评估'myAudio.play()')“

Backbone.js 视图是不是需要 jQuery 或 Zepto? (或者:为啥我会收到“未捕获的类型错误:未定义不是函数”?)

Bourbon Refills Navigation JavaScript 错误:未捕获类型错误:未定义不是函数

骨干本地存储“未定义不是函数”