给定以下Sails.js Controller ,如何从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

最佳答案

重构略有修正:

/**
 * 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 };之类的方法,该方法也不起作用)。

08-16 07:06