AngularJS错误.success不是一个函数

前端之家收集整理的这篇文章主要介绍了AngularJS错误.success不是一个函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
遵循MEAN堆栈书我正在构建一个用户CRM应用程序.我已经建立一个工厂来处理我的控制器的功能,但是不知何故控制器返回一个函数错误
Error: Auth.getUser(...).success is not a function
@http://localhost:8080/app/controllers/mainCtrl.js:10:1
lf/this.$get</n.prototype.$broadcast@http://localhost:8080/assets/libs/angular/angular.min.js:136:43
l@http://localhost:8080/assets/libs/angular-route/angular-route.min.js:10:108
lf/this.$get</n.prototype.$broadcast@http://localhost:8080/assets/libs/angular/angular.min.js:136:43
hf/this.$get</</<@http://localhost:8080/assets/libs/angular/angular.min.js:106:44
lf/this.$get</n.prototype.$eval@http://localhost:8080/assets/libs/angular/angular.min.js:133:217
lf/this.$get</n.prototype.$digest@http://localhost:8080/assets/libs/angular/angular.min.js:130:225
lf/this.$get</n.prototype.$apply@http://localhost:8080/assets/libs/angular/angular.min.js:133:510
zc/d/<@http://localhost:8080/assets/libs/angular/angular.min.js:20:57
e@http://localhost:8080/assets/libs/angular/angular.min.js:39:191
zc/d@http://localhost:8080/assets/libs/angular/angular.min.js:19:1
zc@http://localhost:8080/assets/libs/angular/angular.min.js:20:274
Zd@http://localhost:8080/assets/libs/angular/angular.min.js:19:83
@http://localhost:8080/assets/libs/angular/angular.min.js:293:238
a@http://localhost:8080/assets/libs/angular/angular.min.js:174:399
Hf/c@http://localhost:8080/assets/libs/angular/angular.min.js:35:212
e/<()angular.min.js (line 107)
Ze/this.$get</<()angular.min.js (line 80)
lf/this.$get</n.prototype.$broadcast()angular.min.js (line 136)
l()angular....min.js (line 10)
lf/this.$get</n.prototype.$broadcast()angular.min.js (line 136)
hf/this.$get</</<()angular.min.js (line 106)
lf/this.$get</n.prototype.$eval()angular.min.js (line 133)
lf/this.$get</n.prototype.$digest()angular.min.js (line 130)
lf/this.$get</n.prototype.$apply()angular.min.js (line 133)
zc/d/<()angular.min.js (line 20)
e()angular.min.js (line 39)
zc/d()angular.min.js (line 19)
zc()angular.min.js (line 20)
Zd()angular.min.js (line 19)
angular.min.js()angular.min.js (line 293)
a()angular.min.js (line 174)
Hf/c()angular.min.js (line 35)
angular.min.js (line 107)
GET http://localhost:8080/app/views/pages/home.html

304 Not Modified
        5ms 
angular.min.js (line 93)

我不知道这里发生了什么,其余的功能似乎工作正常吗?

我的主要应用文件

angular.module('userApp',[
    'ngAnimate','app.routes','authService','mainCtrl','userCtrl','ngRoute','userService'
    ]);

authService:

angular.module('authService',[])
    .factory('Auth',function($http,$q,AuthToken) {
        //create auth factory object
        var authFactory = {};
        //handle login
        authFactory.login = function(username,password) {
            //return promise object + data
            return $http.post('/api/authenticate',{
                username: username,password: password
            })
            .success(function(data) {
                AuthToken.setToken(data.token);
                return data;
            });
        };
        //handle logout
        authFactory.logout = function() {
            AuthToken.setToken();
        };
        //check if a user is logged in (is there a local token)
        authFactory.isLoggedIn = function() {
            if(AuthToken.getToken())
                return true;
            else
                return false
        };
        //get the user info
        authFactory.getUser = function() { 
            if (AuthToken.getToken())
                return $http.get('/api/me',{ cache: true });
            else
                return $q.reject({ message: 'User has no token'});
        };
        //return auth factory object
        return authFactory;
    })
    .factory('AuthToken',function($window) {
        var authTokenFactory = {};
        //get the token
        authTokenFactory.getToken = function() {
            return $window.localStorage.getItem('token');
        };
        //set the token or clear the token
        authTokenFactory.setToken = function(token) {
            if(token)
                $window.localStorage.setItem('token',token);
            else
                $window.localStorage.removeItem('token');
        };
        return authTokenFactory;
    })
    .factory('AuthInterceptor',function($q,AuthToken) {
        var interceptorFactory = {};
        //attach the token to every request
        interceptorFactory.request = function(config) {
            //grab the token
            var token = AtuhToken.getToken();
            //if the token exists,add it to the header as x-access-token
            if(token)
                config.header['x-access-token'] = token;
            return config;
        };
        //happens on response errors
        interceptorFactory.responseError = function(response) {
            //if our server returns a 403 forbidden response
            if(response.status = 403) {
                AuthToken.setToken();
                $location.path('login');
            }
            //return the errors from the server as a promise
            return $q.reject(response);
        };
        return interceptorFactory;
    });

主控制器

angular.module('mainCtrl',[])
.controller('mainController',function($rootScope,$location,Auth) {
    var vm = this;
    //check if a person is logged in
    vm.loggedIn = Auth.isLoggedIn();
    //check to see if a user is logged in on every request
    $rootScope.$on('$routeChangeStart',function() {
        vm.loggedIn = Auth.isLoggedIn();
        //get user info on route change
        Auth.getUser()
            .success(function(data) {
                vm.user = data;
            });
    });
    //handle login form
    vm.doLogin = function() {
        //call Auth.login function
        Auth.login(vm.loginData.username,vm.loginData.password)
            .success(function(data) {
                //redirect to users page
                $location.path('/users');
            });
    };
    //handle logging out
    vm.dologout = function() {
        Auth.logout();
        //reset all user info
        vm.user = {};
        $location.path('/login');
    };
});

我也有一个git repository与我的代码(角部分在/公用文件夹).

任何建议/反馈赞赏,我正在学习有角度,仍然不知道如何处理这个.

请参阅 $http service documentation中的“弃用通知”:

The $http legacy promise methods success and error have been
deprecated. Use the standard then method instead.

您可以在documentation about $q中了解有关这些方法的更多信息.

原文链接:https://www.f2er.com/angularjs/143007.html

猜你在找的Angularjs相关文章