2014-04-24 56 views
3

鑑於以下Sails.js控制器,我怎麼能從其processRegistration方法調用它的processLogin方法?如何從同一控制器的另一個函數調用Sails.js控制器的函數?

當前在線56(AuthController.processLogin(req, res);)產生ReferenceError: AuthController is not defined

如果我將其更改爲this.processLogin(req, res);,我收到TypeError: Object #<Object> has no method 'processLogin'

同樣,如果我改變它只是processLogin(req, res);我得到ReferenceError: processLogin is not defined

/** 
* AuthController.js 
* 
* @description :: 
* @docs  :: http://sailsjs.org/#!documentation/controllers 
*/ 

var passport = require('passport'); 

module.exports = { 
    login: function (req, res) { 
    res.view('auth/login'); 
    }, 

    processLogin: function (req, res) { 
    passport.authenticate('local', function (err, user, info) { 
     if ((err) || (!user)) { 
     res.redirect('/login'); 
     return; 
     } 
     req.logIn(user, function (err) { 
     if (err) res.redirect('/login'); 
     console.log(user); 
     console.log('user ' + user.username + ' logged in'); 
     return res.redirect('/user'); 
     }); 
    })(req, res); 
    }, 

    logout: function (req, res) { 
    var username = req.user.username; 
    req.logout(); 
    console.log('user ' + username + ' logged out'); 
    res.redirect('/login'); 
    }, 

    register: function(req,res) { 
    res.view('auth/register'); 
    }, 

    processRegistration: function(req, res) { 
    var userObj = { 
     username: req.param('username'), 
     password: req.param('password') 
    }; 

    User.create(userObj, function(err, user) { 
     if (err) { 
     console.log(err); 
     return res.redirect('/register'); 
     } 

     console.log('user ' + user.username + ' registered'); 

     // Log user in 
     AuthController.processLogin(req, res); // ReferenceError here 

    }); 
    }, 

    _config: {} 
}; 

我正在使用Sails.js 0.10.0-rc5

+1

嘗試module.exports.processLogin – aembke

+0

我想我得到它(見我的答案)。這也適用。謝謝! – blah238

+1

沒問題。提升需要一些習慣。認爲你可以引用一個外部變量,乍一看還沒有完全實例化,這很奇怪。 – aembke

回答

2

重構稍微固定它:

/** 
* AuthController.js 
* 
* @description :: 
* @docs  :: http://sailsjs.org/#!documentation/controllers 
*/ 

var passport = require('passport'); 

var AuthController = { 
    login: function(req, res) { 
    res.view('auth/login'); 
    }, 

    processLogin: function(req, res) { 
    passport.authenticate('local', function(err, user, info) { 
     if ((err) || (!user)) { 
     res.redirect('/login'); 
     return; 
     } 
     req.logIn(user, function(err) { 
     if (err) res.redirect('/login'); 
     console.log('user ' + user.username + ' logged in'); 
     return res.redirect('/user'); 
     }); 
    })(req, res); 
    }, 

    logout: function(req, res) { 
    var username = req.user.username; 
    req.logout(); 
    console.log('user ' + username + ' logged out'); 
    res.redirect('/login'); 
    }, 

    register: function(req, res) { 
    res.view('auth/register'); 
    }, 

    processRegistration: function(req, res) { 
    var userObj = { 
     username: req.param('username'), 
     password: req.param('password') 
    }; 

    User.create(userObj, function(err, user) { 
     if (err) { 
     console.log(err); 
     return res.redirect('/register'); 
     } 

     console.log('user ' + user.username + ' registered'); 

     // Log user in 
     AuthController.processLogin(req, res); 

    }); 
    } 
}; 

module.exports = AuthController; 

我以前試過,但只是沒有得到module.exports行權(不喜歡的東西module.exports = { AuthController: AuthController };,這也不能工作)。

+0

那麼你如何在路線中調用它 – klaudia

1

我知道這是舊的,但sails.controllers.公開控制器

相關問題