我正在尝试实现passport.js,问题是在模型中导出函数的方式:
用户模型文件(User .js)看起来像:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');
var bcrypt = require('bcrypt');
var userSchema = mongoose.Schema({
username: String,
password: String
});
userSchema.plugin(passportLocalMongoose);
userSchema.methods = {
getUserByUsername: function(username, callback){
var query = {username: username};
userSchema.findOne(query, callback);
/*userSchema.findOne(query, function(err, user) {
callback(err, user);
}); */
},
getUserById: function(id, callback){
userSchema.findById(id, callback);
},
comparePassword: function(candidatePassword, hash, callback){
bcrypt.compare(candidatePassword, hash, function(err, isMatch) {
if(err) throw err;
callback(null, isMatch);
});
}
}
module.exports = mongoose.model('User', userSchema);
我调用模型(app.js):
// user schema/model
var User = require('./models/user.js');
和我试图使用导出的功能在护照(app.js):
passport.use(new localStrategy(
function(username, password, done) {
User.getUserByUsername(username, function(err, user){
if(err) throw err;
if(!user){
return done(null, false, {message: 'Unknown User'});
}
User.comparePassword(password, user.password, function(err, isMatch){
if(err) throw err;
if(isMatch){
return done(null, user);
} else {
return done(null, false, {message: 'Invalid password'});
}
});
});
}));
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.getUserById(id, function(err, user) {
done(err, user);
});
});
我得到TypeError: User.getUserByUsername is not a function
我试图定义一个关于docs的函数:
User.methods.getUserByUsername = function(username, callback){
var query = {username: username};
User.findOne(query, callback);
}
和相同的错误出现在控制台中,当我试图登录…
编辑:我说:
var User = mongoose.model('User', userSchema);
module.exports = {
User: User
};
现在它与方法定义一起工作:
module.exports.getUserByUsername()
所以最终的模型文件看起来像:
// user model
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');
var bcrypt = require('bcrypt');
var userSchema = mongoose.Schema({
username: String,
password: String
});
userSchema.plugin(passportLocalMongoose);
var User = mongoose.model('User', userSchema);
module.exports = {
User: User
};
module.exports.createUser = function(newUser, callback){
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(newUser.password, salt, function(err, hash) {
newUser.password = hash;
newUser.save(callback);
});
});
}
module.exports.getUserByUsername = function(username, callback){
var query = {username: username};
User.findOne(query, callback);
}
module.exports.getUserById = function(id, callback){
User.findById(id, callback);
}
module.exports.comparePassword = function(candidatePassword, hash, callback){
bcrypt.compare(candidatePassword, hash, function(err, isMatch) {
if(err) throw err;
callback(null, isMatch);
});
}
谢谢大家的帮助
代替
module.exports.getUserByUsername = ...
或
User.methods.getUserByUsername = ...
使用User.statics.getUserByUsername = ...
见http://mongoosejs.com/docs/2.7.x/docs/methods-statics.html
如果你是NodeJS的新手,那么理解模块。exports和exports可能有点混淆。清楚地了解这个概念是很重要的,因为如果你还在认真地进行nodejs开发,你会经常遇到这个问题。我不打算在这里解释在nodejs中导出是如何工作的,因为在web中有很多教程。如;
按照我下面的代码来解决你的问题。
//UserModel.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt');
var passportLocalMongoose = require('passport-local-mongoose');
var userSchema = mongoose.schema({
username: String,
password: String
});
userSchema.plugin(passportLocalMongoose);
userSchema.methods = {
createUser: function(...){..},
getUserByUsername: function(..){..},
getUserById: function(..){..},
comparePassword: function(..),{}
}
module.exports = mongoose.model('User', userSchema);
你可以从你的UserSchema中访问方法:
//Controller.js
var User = require('path/to/UserModel.js');
User.createUser(..,..)
更多细节,你可以参考我的仓库,看看我是怎么做的。