Elly Bolson Elly Bolson - 3 months ago 6x
Node.js Question

Why can't I create and execute functions in my passport.js local strategy callback?

When I try calling

it results in an error saying
usr is not defined
. I just don't get it and why it is not accessible? I know I can simply exclude the function and have the functions contents in the callback and it would work. I just want to know a way that I can declare the usr object and call that through a function in the callback. Exactly the way I tried in my callback. Is that possible? and why does my method not work?

var LocalStrategy = require('passport-local').Strategy;
var User = require('../app/models/user');

function saveTheUsr(){
var usr = new User();
usr.local.email = email;
usr.local.password = password;
usr.save(function(err) {
if (err)
throw err;
return done(null, usr);

passport.use('local-signup', new LocalStrategy({
usernameField : 'username',
passwordField : 'password',
passReqToCallback : true
function(req, email, password, done) {

User.findOne({ 'local.email' : email }, function(err, user) {

if (err)
return done(err);

if (user) {
return done(null, false);
} else {




As pointed in the comment.

The issue was with the done variable not being defined in the saveTheUsr() function. And the fix for it would be to make saveTheUsr accept an extra parameter called done and pass a callback function for places that calls it.