passport mongoose对象#<schema>没有方法'authenticate'但我可以看到方法</schema>

时间:2014-02-21 03:54:40

标签: javascript node.js mongodb mongoose mongoose-plugins

我遇到了一个没有意义的问题,无法想出有人可以提供协助。

我正在使用一些猫鼬插件,现在也试图整合护照。我已经添加了passport-local-mongoose插件,如下所示,但是我收到的错误是该方法不存在。将模式记录到控制台时,我可以看到列出的方法,所以我不确定它为什么不存在。

代码示例:

var mongoose = require('mongoose');
var timestamps = require('mongoose-timestamp');
var autoIncrement = require('mongoose-auto-increment');
var passport = require('passport');
var passportLocalMongoose = require('passport-local-mongoose');
var BasicStrategy = require('passport-http').BasicStrategy;

var usersSchema = new mongoose.Schema({
  firstName: String,
  lastName: String,
  email: String,
  organizationId: Number,
  description: String
});

module.exports = function(app,db){
  //mongoose setup options
  usersSchema.plugin(timestamps);  
  usersSchema.plugin(autoIncrement.plugin, { model: 'Users', field: 'id' });

  //passport authentication setup
  usersSchema.plugin(passportLocalMongoose);
  var Users = db.model('Users', usersSchema);
  console.log(usersSchema);

  passport.use(new BasicStrategy(usersSchema.authenticate()));

控制台日志摘录如下:

methods:
 { nextCount: [Function],
   resetCount: [Function],
   setPassword: [Function],
   authenticate: [Function] },
statics:
 { nextCount: [Function],
   resetCount: [Function],
   authenticate: [Function],
   serializeUser: [Function],
   deserializeUser: [Function],
   register: [Function],
   findByUsername: [Function],
   createStrategy: [Function] },

但是我收到以下错误:

passport.use(new BasicStrategy(usersSchema.authenticate()));
                                             ^
TypeError: Object #<Schema> has no method 'authenticate'

我可以看到该方法可用,为什么我无法访问它。如果我遗漏了一些基本的东西,我会道歉,我还在学习其中的一些东西。感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

authenticate方法是在模型上定义的,不在模式上 -

passport.user(new  BasicStrategy(Users.authenticate());