Reputation: 31
When I try calling saveTheUsr()
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 {
saveTheUsr();
}
});
}));
Upvotes: 0
Views: 29
Reputation: 19278
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.
Upvotes: 2