简体   繁体   中英

error using then and catch in angularjs

I am trying to create an user authentication using Flask as backend and AngularJS as frontend. But I'm stuck at this error. Below is the Angular code that I am using. The login function executes successfully but also throws the following error. Please help me in solving/figuring this issue.

'use strict';

var userModule = angular.module('userModule', ['userServices']);

userModule.controller('LoginController', ['$scope', '$location', 'userAuth', function($scope, $location, userAuth) {

$scope.login = function() {
    console.log("username: " + $scope.email + ", password: " + $scope.password);
    // userAuth.login($scope.username, $scope.password);

    // initial values
  $scope.error = false;
  $scope.disabled = true;

  // call login from service
  userAuth.login($scope.email, $scope.password)
  // handle success
  .then(function () {
    console.log('I am in then function');
  })
  .catch(function () {

  })
};

}]);

Error

TypeError: userAuth.login(...).then(...).catch is not a function
at Object.$scope.login          (http://localhost:5000/static/js/controllers/user.js:28:13)
at http://localhost:5000/static/lib/angular/angular.js:6365:19
at Object.Scope.$eval   (http://localhost:5000/static/lib/angular/angular.js:8057:28)
at Object.Scope.$apply (http://localhost:5000/static/lib/angular/angular.js:8137:23)
at HTMLFormElement.<anonymous> (http://localhost:5000/static/lib/angular/angular.js:13159:11)
at http://localhost:5000/static/lib/angular/angular.js:1992:10
at Array.forEach (native)
at forEach (http://localhost:5000/static/lib/angular/angular.js:130:11)
at HTMLFormElement.eventHandler (http://localhost:5000/static/lib/angular/angular.js:1991:5)

and my login function

function login(email, password) {

// create a new instance of deferred
var deferred = $q.defer();

// send a post request to the server
$http.get('http://' + email + ':' + password + '@localhost:5000/api/login')
  // handle success
  .success(function (data, status) {
    if(status === 200 && data.result){

      // print response
      console.log('Response: ' + JSON.stringify(data))

      user = true;
      deferred.resolve();
    } else {

      // print response
      console.log('Response: ' + JSON.stringify(data))

      user = false;
      deferred.reject();
    }
  })
  // handle error
  .error(function (data) {

    // print response
    console.log('Response: ' + JSON.stringify(data))

    user = false;
    deferred.reject();
  });

// return promise object
return deferred.promise;

  }

I am tired of searching and figuring this issue but didn't find anything. Don't know what's wrong :/ Any help will be appreciated.

try this

userAuth.login($scope.email, $scope.password)
 .then(function () {
    console.log('I am in then function');
 },
 function(error){
    console.log('Error', error);
 })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM