TypeError:无法通过Interceptor读取Angular中未定义的属性“数据”

ole*_*ank 5 javascript typeerror interceptor angularjs

调用RESTful服务时,我有一个拦截器来捕获HTTP错误:

services.config(function($httpProvider) {
    $httpProvider.responseInterceptors.push('globalInterceptor');

    var elementsList = $();

    // this message will appear for a defined amount of time and then vanish again
    var showMessage = function(content, cl, time) {
        $('<div/>')
            .addClass(cl)
            .hide()
            .fadeIn('fast')
            .delay(time)
            .fadeOut('fast', function() { $(this).remove(); })
            .appendTo(elementsList)
            .text(content);
    };
});

services.factory('globalInterceptor', function($q){
    //When the interceptor runs, it is passed a promise object
    return function(promise){

        //In an interceptor, we return another promise created by the .then function.
        return promise.then(function(successResponse){
            //Do your code here if the response was successful
            if (typeof(successResponse) !== 'undefined') {
                if (successResponse.config.method.toUpperCase() != 'GET') {
                    showMessage('Success', 'http-success-message', 5000);
                    return successResponse;
                }
            }
        }, function(errorResponse){
            switch (errorResponse.status) {
                case 400: // if the status is 400 we return the error
                    showMessage(errorResponse.data.message, 'http-error-message', 6000);
                    // if we have found validation error messages we will loop through
                    // and display them
                    if(errorResponse.data.errors.length > 0) {
                        for(var i=0; i<errorResponse.data.errors.length; i++) {
                            showMessage(errorResponse.data.errors[i],
                                'http-error-validation-message', 6000);
                        }
                    }
                    break;
                case 401: // if the status is 401 we return access denied
                    showMessage('Wrong email address or password!',
                        'http-error-message', 6000);
                    break;
                case 403: // if the status is 403 we tell the user that authorization was denied
                    showMessage('You have insufficient privileges to do what you want to do!',
                        'http-error-message', 6000);
                    break;
                case 500: // if the status is 500 we return an internal server error message
                    showMessage('Internal server error: ' + errorResponse.data.message,
                        'http-error-message', 6000);
                    break;
                default: // for all other errors we display a default error message
                    showMessage('Error ' + errorResponse.status + ': ' + errorResponse.data.message,
                        'http-error-message', 6000);
            }

            return $q.reject(errorResponse);
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

加载时我的登录页面停止。问题出在确定接收器之后,然后一切正常。

它给了我这个错误消息,实际上并没有帮助:

TypeError: Cannot read property 'data' of undefined
    at http://localhost:8080/vendor/angular-1.2.2/angular.js:7479:22
    at deferred.promise.then.wrappedCallback (http://localhost:8080/vendor/angular-1.2.2/angular.js:10655:81)
    at deferred.promise.then.wrappedCallback (http://localhost:8080/vendor/angular-1.2.2/angular.js:10655:81)
    at http://localhost:8080/vendor/angular-1.2.2/angular.js:10741:26
    at Scope.$get.Scope.$eval (http://localhost:8080/vendor/angular-1.2.2/angular.js:11634:28)
    at Scope.$get.Scope.$digest (http://localhost:8080/vendor/angular-1.2.2/angular.js:11479:31)
    at Scope.$get.Scope.$apply (http://localhost:8080/vendor/angular-1.2.2/angular.js:11740:24)
    at done (http://localhost:8080/vendor/angular-1.2.2/angular.js:7744:45)
    at completeRequest (http://localhost:8080/vendor/angular-1.2.2/angular.js:7918:7)
    at XMLHttpRequest.xhr.onreadystatechange (http://localhost:8080/vendor/angular-1.2.2/angular.js:7874:11) angular.js:9159
Run Code Online (Sandbox Code Playgroud)

我试过debug / console.log这个“数据”属性,但是找不到它...当应用程序加载时,它会得到两个文件OK(200个,数据属性为attr)和其中两个错误。

任何帮助将不胜感激:-)

小智 0

我没有看到你的函数有任何问题(errorResponse)有任何问题。它似乎正确地沿着链传递了错误。

但我对你的功能有疑问(successResponse)。仅当满足两个 IF 语句时,它才会将值沿链传递,在其他情况下,它会传递未定义的值。

我的猜测是您需要在function(successResponse)的末尾添加 $q.reject() 。