Не могу вернуть более одного параметра по ошибке

У меня есть проект, где я использую Node/Express для API и Angular в интерфейсе.

У меня есть конечная точка, где я хочу вернуть более одного поля, чтобы заполнить сообщение об ошибке.

Вот текущий код.post у меня есть:

router.route('/')
.post(function(req, res) {

  User.findOne({
    login: req.body.login
  }, function(err, user) {
    if (err)
      res.send(err);

    if (!user) {
      res.send({success: false, msg: 'Authentication failed. User not found.'});
    } else {
      console.log('User' + user.confirmed)
      // check if password matches
      user.comparePassword(req.body.password, function (err, isMatch) {
        if (isMatch && !err) {

          if(user.confirmed){
            // if user is found and password is right create a token
            var token = jwt.encode(user, config.secret);
            // return the information including token as JSON
            res.json({success: true, token: 'JWT ' + token});
          } else {
            res.json({success: false, msg: 'In order to login, you must first confirm you email. A confirmation email was sent to ' + user.email })
          }

        } else {
          res.send({success: false, msg: 'Authentication failed. Wrong password.'});
        }
      });
    }
  });
});

В "success: false" я хочу вернуть заголовок сообщения об ошибке и сообщение об ошибке, которое я затем выведу на Angular.

Я попытался использовать "res.json", а также "res.send".

Я добавил поле "заголовок" в свой ответ, например так:

 res.json({success: false, title:'Email confirmation needed', msg: 'In order to login, you must first confirm you email. A confirmation email was sent to ' + user.email })
              }

Но когда я попытался получить к нему доступ с угловой стороны, используя "err.title" и "err.msg", он не был определен.

Вот мой угловой контроллер:

.controller('LoginController', function($scope, $rootScope, $state, localStorageService, SweetAlert, AuthService){

  $scope.user = {
    name: '',
    password: ''
  };

  $scope.login = function() {
    AuthService.login($scope.user).then(function(msg) {
      $state.go('dashboard');
    }, function(err) {
      console.log('There was an error:', err);

      SweetAlert.swal({
          title: 'Login Error',
          text: err,
          type: 'warning',
          showCancelButton: true,
          confirmButtonColor: '#DD6B55',
          confirmButtonText: 'Retry',
          closeOnConfirm: true
      }, function() { $state.go('login'); });

    });
  };


});

Любая помощь будет оценена!!

0 ответов

Другие вопросы по тегам