如何从同一控制器的另一个函数调用Sails.js控制器函数?

时间:2014-04-24 20:12:53

标签: javascript node.js sails.js

鉴于以下Sails.js控制器,如何从processLogin方法调用其processRegistration方法?

目前,这会在第56行(ReferenceError: AuthController is not defined)生成AuthController.processLogin(req, res);

如果我将其更改为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

2 个答案:

答案 0 :(得分: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 };之类的内容,但这也没有用。)

答案 1 :(得分:1)

我知道这是旧的,但是sails.controllers.暴露了控制器