Catch undefined TypeError from use of 'eval' in Angular?

533 views Asked by At

I'm getting a console error that I can't seem to clear. Does anyone have suggestions for how to catch/clear this?

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)

I've narrowed down the error to be either from my use of eval on my last line of code, or because of the order of operations this function performs. I tried catching the error with an if (scope.metric !== undefined), but that didn't work.

In the context of this issue, 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); })]);
}
1

There are 1 answers

4
Ilia Choly On

Instead of using eval, you can access the property using bracket notation

eval('d.' + scope.metric)

// should be written as

d[scope.metric]