Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Passport LocaStrategy注册重定向问题_Javascript_Express_Mongoose_Passport.js_Mean Stack - Fatal编程技术网

Javascript Passport LocaStrategy注册重定向问题

Javascript Passport LocaStrategy注册重定向问题,javascript,express,mongoose,passport.js,mean-stack,Javascript,Express,Mongoose,Passport.js,Mean Stack,我正在尝试在注册Passport后重定向用户 passport.use('local-signup', new LocalStrategy({ usernameField : 'username', passwordField : 'password', passReqToCallback : true}, function(req, username, password, done) { // asynchronous // User.findOne wont fire un

我正在尝试在注册Passport后重定向用户

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

    // asynchronous
    // User.findOne wont fire unless data is sent back
    process.nextTick(function() {

    // find a user whose username is the same as the forms username
    User.findOne({ username :  username }, function(err, user) {
        // if there are any errors, return the error
        if (err)
            return done(err);
        if (user) {
            return done(null, false, req.flash('signupMessage', 'Username is already taken.'));
        } else {
            var newUser = new User();
            newUser.username = username;
            newUser.password = password;
            newUser.email = req.body.email;
            newUser.save(function(err) { // create user
                if (err) {
                    return next(err);
                } else {
                    var site = new Site({user : newUser.username, siteTitle: newUser.username});
                    site.save(function(err) { // create website
                        if (err) {
                            return next(err);
                        } else {
                        newUser.sites.push(site); // push site'id in sites field in user
                        newUser.save(); // save user after site'id has been push
                        };
                    });
                };
            res.redirect('users/' + username);
            });
            ...
我得到了ReferenceError:res没有定义

任何帮助将不胜感激,因为我开始意味着开发


非常感谢

您不应该在这里处理重定向-策略的功能是确定用户是否经过身份验证

然后可以在中间件中执行重定向:

app.post('/login', passport.authenticate('local', {
                     successRedirect: '/',
                     failureRedirect: '/login'
}));

不要使用
res.redirect(..)
,使用正常的
done
回调,然后适当地处理响应。