简体   繁体   English

3angular.js:13920 TypeError:无法设置未定义的属性'contract'

[英]3angular.js:13920 TypeError: Cannot set property 'contract' of undefined

function loadAll() {
            Organization.query({`enter code here`
                page: pagingParams.page - 1,
                size: vm.itemsPerPage,
                sort: sort(),
                mapperType: 'NORMAL',
                status: 'DISABLED',
                search: vm.search
            }, onSuccess, onError);

        function sort() {
            var result = [vm.predicate + ',' + (vm.reverse ? 'asc' : 'desc')];
            if (vm.predicate !== 'id') {
                result.push('id');
            }
            return result;
        }

        function onSuccess(data, headers) {
            vm.links = ParseLinks.parse(headers('link'));
            vm.totalItems = headers('X-Total-Count');
            vm.queryCount = vm.totalItems;
            vm.page = pagingParams.page;
            vm.searchParams = PrmJson.fromJson(pagingParams.search);
            // vm.organizations = data;
            vm.organizations = getContractStatus(data);
        }

        function onError(error) {
            AlertService.error(error.data.message);
        }
    }

    function getContractStatus(organizations) {
        for (var i = 0; i < organizations.length; i++) {
            Contract.getByOrganizationId({
                    organizationId: organizations[i].id
                }, function (data) {
                    if (data.length == 0) {
                        organizations[i]["contract"] = {id: 0};
                    } else {
                        organizations[i]["contract"] = {id: data.id};
                    }
                }
            );
        }

        return organizations;
    }

When I run the code above I get the following error message: 当我运行上面的代码时,我收到以下错误消息:

angular.js:13920 TypeError: Cannot set property 'contract' of undefined angular.js:13920 TypeError:无法设置未定义的属性'contract'

You are passing anonymous function, so there data and i value both are undefined . 您正在传递匿名函数,因此datai值都未定义。

function(data,i) {
  if (data.length == 0) {
    organizations[i]["contract"] = {
      id: 0
    };
  } else {
    organizations[i]["contract"] = {
      id: data.id
    };
  }
}(data,i);

Demo 演示版

 for (var i = 0; i <= 5; i++) { demo1(i, function(k,l) { console.log('annonymous',l) }(i,i)); } function demo1(val) { console.log('demo1',val) } 

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

相关问题 angular.js:13920 TypeError:无法读取null的属性&#39;push&#39; - angular.js:13920 TypeError: Cannot read property 'push' of null angular.js:13550 TypeError:无法设置未定义的属性“people” - angular.js:13550 TypeError: Cannot set property 'people' of undefined Angular TypeError 无法设置未定义的属性“数据” - Angular TypeError cannot set property 'Data' of undefined 无法设置未定义角度js的属性 - cannot set property of undefined angular js $ window.location.href(angular.js:12520 TypeError:无法设置未定义的属性&#39;href&#39;) - $window.location.href (angular.js:12520 TypeError: Cannot set property 'href' of undefined) “未捕获的TypeError:无法读取Websocket Angular JS中未定义的属性&#39;defer&#39;” - “Uncaught TypeError: Cannot read property 'defer' of undefined at Websocket Angular JS” angular.js:14110 TypeError:无法读取undefined的属性&#39;push&#39; - angular.js:14110 TypeError: Cannot read property 'push' of undefined Angular.js:10671 TypeError:无法读取未定义的属性 - Angular.js:10671 TypeError: Cannot read property of undefined TypeError:无法设置未定义的属性“ - TypeError: Cannot set property '' of undefined 错误类型错误:无法设置未定义 ANGULAR 的属性“名称” - Error TypeError: cannot set property “name” of undefined ANGULAR
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM