使用Angular时如何调试模糊的JS错误?

时间:2016-08-30 17:30:26

标签: javascript angularjs debugging angular-ui-grid

我最近开始使用Angular,到目前为止,我很喜欢错误消息中的链接,这些链接可以为您提供有关错误的令人难以置信的详细信息。

但是,我遇到了一个非常模糊的JavaScript错误:

TypeError: Cannot read property 'data' of undefined
    at new <anonymous> (ui-grid.min.js:7)
    at Object.invoke (angular.js:4709)
    at R.instance (angular.js:10234)
    at m (angular.js:9147)
    at angular.js:9553
    at angular.js:16170
    at m.$eval (angular.js:17444)
    at m.$digest (angular.js:17257)
    at m.$apply (angular.js:17552)
    at l (angular.js:11697)
    (anonymous function) @ angular.js:13708
    (anonymous function) @ angular.js:10347
    (anonymous function) @ angular.js:16178
    $eval @ angular.js:17444
    $digest @ angular.js:17257
    $apply @ angular.js:17552l 
    @ angular.js:11697K 
    @ angular.js:11903y.onload 
    @ angular.js:11836

因为我正在使用ui-grid,data在一百个地方,我试图一个接一个地查看是否有任何东西看起来,但它变得如此乏味。

如果错误中没有引用我编写的代码,那么调试此错误的好方法是什么?

  

注意:我不是问如何解决这个特定的错误,甚至是这个错误   错误意味着我在问如何找到我的代码的哪一部分   正在引用。

2 个答案:

答案 0 :(得分:0)

该错误通常意味着您未正确定义数据对象,或者错误定义了对象。这是一个标准的javascript错误,它说它试图找到属性&#34;数据&#34;对于在尝试访问它时未定义的对象。

答案 1 :(得分:0)

首先,您正在使用ui-grid.min.js,这对调试没什么帮助。使用有助于调试的ui-grid.js。

为了帮助解决数据未定义的问题,您是否按以下方式使用它?您可以发布一些代码以获得更多帮助..

UI Grid从您定义的网格选项中读取数据属性。

$scope.gridOptions = {
    showGridFooter: true,
    showColumnFooter: true,
    enableFiltering: true,
    columnDefs: [
        { field: 'name', width: '13%' },
        { field: 'address.street',aggregationType: uiGridConstants.aggregationTypes.sum, width: '13%' },
        { field: 'age', aggregationType: uiGridConstants.aggregationTypes.avg, aggregationHideLabel: true, width: '13%' },
        { name: 'ageMin', field: 'age', aggregationType: uiGridConstants.aggregationTypes.min, width: '13%', displayName: 'Age for min' },
        { name: 'ageMax', field: 'age', aggregationType: uiGridConstants.aggregationTypes.max, width: '13%', displayName: 'Age for max' },
        { name: 'customCellTemplate', field: 'age', width: '14%', footerCellTemplate: '<div class="ui-grid-cell-contents" style="background-color: Red;color: White">custom template</div>' },
        { name: 'registered', field: 'registered', width: '20%', cellFilter: 'date', footerCellFilter: 'date', aggregationType: uiGridConstants.aggregationTypes.max }
    ],
    data: data,
    onRegisterApi: function(gridApi) {
            $scope.gridApi = gridApi;
    }
};

上面的网格选项将数据作为您的网格数据属性之一。

 <div id="grid1" ui-grid="gridOptions" class="grid"></div>

这是网格选项连接到模板中网格的方式。

相关问题