我收到一个似乎无法清除的控制台错误。有人对如何捕捉/清除这个有建议吗?
TypeError: Cannot read property 'total' of undefined at Array.eval (eval at (http://brandfeed.app:8001/app/scripts/directives/report-chart.js:133:28), :1:20)
我已经将错误范围缩小到要么是由于我在最后一行代码中使用了 eval
,要么是由于该函数执行的操作顺序所致。我尝试使用 if (scope.metric !== undefined)
捕获错误,但这不起作用。
在此问题的上下文中,scope.metric
=== impressions.total
。
if ((scope.metric !== undefined) &&(scope.metric !== "")) {
var maximumY = d3.max(data, function(d) {
return eval('d.' + scope.metric);
});
x.domain(data.map(function(d) { return d.id; }));
y.domain([-(maximumY * .01), d3.max(data, function(d) { return eval('d.' + scope.metric); })]);
}
最佳答案
您可以使用括号表示法访问属性,而不是使用eval
eval('d.' + scope.metric)
// should be written as
d[scope.metric]
关于javascript - 在 Angular 中使用 'eval' 捕获未定义的 TypeError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30876631/