在 AngularJS 中使用 then 和 catch 时出错
error using then and catch in angularjs
我正在尝试使用Flask作为后端和AngularJS作为前端来创建用户身份验证。但是我被这个错误困住了。下面是我正在使用的 Angular 代码。登录函数成功执行,但也引发以下错误。请帮助我解决/解决这个问题。
'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 () {
})
};
}]);
错误
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)
和我的登录功能
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;
}
我厌倦了搜索和解决这个问题,但没有找到任何东西。不知道怎么了:/任何帮助将不胜感激。
试试这个
userAuth.login($scope.email, $scope.password)
.then(function () {
console.log('I am in then function');
},
function(error){
console.log('Error', error);
})
相关文章:
- javascript中的数组出错
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- 在Meteor项目中安装并包含npm模块后出错
- 解析JSON并从中删除对象会出错
- AngularJS指令出错-无法读取属性'编译'的未定义
- 使用javascript在MVC中查找网格长度时出错
- 尝试调用AngularJS上的函数时出错
- Google 脚本:用于创建日历活动的脚本运行时不会出错,但不会执行任何操作
- 全局安装gull后出错
- 我在代码点火器中出错
- Java脚本将URL转换为已保存的URL时出错
- Backbone.js将模型绑定到视图时出错
- 将javascript代码转换为jquery代码时出错
- errors with Javascript try catch
- 加载db.json时出错
- 在运行时创建元素时移到一边时出错
- 失败:等待Protractor与页面同步时出错:“”;在窗口上找不到角度”;
- 重置表单字段时出错
- 将对象的列表c#传递给javascript:undefined变量时出错
- 在 AngularJS 中使用 then 和 catch 时出错