简体   繁体   English

使用then错误并捕获angularjs

[英]error using then and catch in angularjs

I am trying to create an user authentication using Flask as backend and AngularJS as frontend. 我正在尝试使用Flask作为后端和AngularJS作为前端来创建用户身份验证。 But I'm stuck at this error. 但是我被这个错误困住了。 Below is the Angular code that I am using. 以下是我正在使用的Angular代码。 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);
 })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在angularjs的任何地方捕获错误 - Catch an error anywhere angularjs 如何在AngularJS的输入字段中捕获模式错误? - How to catch pattern error in input field in AngularJS? AngularJS中的promise会捕获每个异常/错误吗? - Do promises in AngularJS catch every exception/error? 在firebase中使用带有.on()的catch()时出错 - Error using catch() with a .on() in firebase 在angularjs中,当localStorage已满时,如何捕获ngStorage错误? - In angularjs how do you catch an error for ngStorage when localStorage is full? 在angularjs $ http.get()调用上catch()不会抑制错误 - catch() on angularjs $http.get() call doesn't suppress error 是否有可能在AngularJS中捕获“由x-frame选项拒绝的负载”错误? - Is it possible to catch a “load denied by x-frame option” error in AngularJS? 如何使用Jasmine测试AngularJS中的.catch Promise返回? - How Can I Test a .catch Promise Return in AngularJS using Jasmine? 如何使用try and catch捕获多个功能来捕获错误? - How to catch an error using try and catch for multiple functions? 将 try-catch 用于 https 但没有发现任何错误 - using try-catch for https but don't catch any error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM