简体   繁体   English

为什么我收到TypeError:onSuccess不是我的angularjs服务中的函数

[英]Why am I geting TypeError: onSuccess is not a function in my angularjs service

I have an angularjs services that leverages restangular to make calls. 我有一个利用restangular进行调用的angularjs服务。

angular.module('app.userSvc', [])
    .factory('userSvc', ['localStorageService', '$rootScope', 'Restangular',
        function(localStorageService, $rootScope, Restangular) {
            function checkIfLoggedIn() {
                return localStorage.getItem('token');
            }

            function login(email, password, onSuccess, onError) {
                Restangular.all('api/authenticate')
                    .post({
                        email: email,
                        password: password
                    })
                    .then(
                        function(response) {
                            localStorage.setItem('token', response.token);
                            onSuccess(response);
                        },
                        function(response) {
                            onError(response);
                        });
            }

            function logout() {
                localStorageService.remove('token');
            }

            function getCurrentToken() {
                return localStorage.getItem('token');
            }

            function getAuthenticatedUser(onSuccess, onError) {
                Restangular.one('api/authenticated_user?token=' + getCurrentToken()).get().then(function(response) {
                    onSuccess(response.user);
                }, function(response) {
                    onError(response);
                });
            }
            Restangular.setDefaultHeaders({
                'Authorization': 'Bearer ' + getCurrentToken()
            });
            return {
                checkIfLoggedIn: checkIfLoggedIn,
                login: login,
                logout: logout,
                getCurrentToken: getCurrentToken,
                getAuthenticatedUser: getAuthenticatedUser
            };
        }
    ]);

But when i call the userSvc.getAuthenticatedUser() method in a controller i get TypeError: onSuccess is not a function This is how am calling it. 但是,当我在控制器中调用userSvc.getAuthenticatedUser()方法时,我得到TypeError:onSuccess不是函数,这就是调用它的方式。

console.log(userSvc.getAuthenticatedUser());

What am i doing wrong. 我究竟做错了什么。 I am using angular 1.6 我正在使用角度1.6

You HAVE to set a callback function as the first argument, because inside getAuthenticatedUser() , there is no check to see whether the onSuccess argument is set or not. 您必须将回调函数设置为第一个参数,因为在getAuthenticatedUser() ,无法检查是否设置了onSuccess参数。 (The same is actually true for the onError callback, so you need to provide that second argument, too) (对于onError回调实际上也是如此,因此您也需要提供第二个参数)

So, either implement such a check (eg onSuccess && onSuccess() ) inside the getAuthenticatedUser function, or provide a callback function (even if it's an anonymous one that doesn't to anything) in order to prevent that error. 因此,可以在getAuthenticatedUser函数内部实现这样的检查(例如onSuccess && onSuccess() ),或者提供一个回调函数(即使它是匿名的,不做任何事情)以防止该错误。

userSvc.getAuthenticatedUser(function() {}, function() {});

Error is expected, As your service method expected callback method so pass them 预计会出现错误,因为您的服务方法应使用回调方法,所以请传递

function getAuthenticatedUser(onSuccess, onError){
}

So you need to pass the callback methods 所以你需要传递回调方法

userSvc.getAuthenticatedUser(function() {}, function() {})

Or, You can check whether argument is defined and a angular.isFunction 或者,您可以检查是否定义了参数和angular.isFunction。

function getAuthenticatedUser(onSuccess, onError) {
    Restangular.one('api/authenticated_user?token=' + getCurrentToken()).get().then(function (response) {
        !!onSuccess && angular.isFunction(onSuccess) && onSuccess(response.user);
    }, function (response) {
        !!onError && angular.isFunction(onError) &&
        onError(response);
    });
}

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

相关问题 我正在使用这个javascript来“未定义”。 为什么? - I am geting “undefined” with this javascript. Why? 为什么我收到TypeError X不是函数 - Why am I getting TypeError X is not a function 为什么我在angularjs中没有出现函数错误? - Why I am getting not a function error in angularjs? 在尝试导入我的函数时,为什么会出现“TypeError:Assignment to constant variable”? - Why am I getting “TypeError: Assignment to constant variable” when trying to import my function? 为什么在尝试运行我的代码时会收到 TypeError? - Why am I getting a TypeError when trying to run my code? 为什么我得到 TypeError: _this.props."functionName" is not a function ReactJS - Why am I getting TypeError: _this.props."functionName" is not a function ReactJS 为什么会出现“未捕获的TypeError:getEnumerator不是函数”? - Why am I getting, “Uncaught TypeError: getEnumerator is not a function”? 为什么我得到TypeError:obj.addEventListener不是函数? - why am I getting TypeError: obj.addEventListener is not a function? 为什么我总是收到“类型错误:回调不是函数” - why am I getting always "TypeError: callback is not a function" 为什么我收到 TypeError: Log.addSales is not a function - Why am I getting TypeError: Log.addSales is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM