javascript - cannot export function in Express -
i'm trying implement passport.js , problem in way of exporting function in model:
user model file (user.js) looks like:
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);
i call model (app.js):
// user schema/model var user = require('./models/user.js');
and i'm trying use exported functions in passport (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); }); });
i got typeerror: user.getuserbyusername not function
i tried define function regarding docs:
user.methods.getuserbyusername = function(username, callback){ var query = {username: username}; user.findone(query, callback); }
and same error apperas in console when i'm trying login..
edit: i've added:
var user = mongoose.model('user', userschema); module.exports = { user: user };
and works methods definition:
module.exports.getuserbyusername()
so final model file looks like:
// 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); }); }
thank guys help.
instead of
module.exports.getuserbyusername = ...
or
user.methods.getuserbyusername = ...
use
user.statics.getuserbyusername = ...
see http://mongoosejs.com/docs/2.7.x/docs/methods-statics.html
Comments
Post a Comment