Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RangeError: Maximum call stack size exceeded

Tags:

angularjs

I guess this means there is a circular reference somehwere but for the life of me I can't guess how to fix it.

Anyone have any ideas?

http://plnkr.co/edit/aNcBcU?p=preview

Check the debug console in Chrome (for example) and you'll see the error. The offending line is

 scope.map = map;

scope.map is being "$watched" on the controller via

$scope.$watch("options.map", function (map) { ... }, true);
like image 289
thrag Avatar asked Apr 12 '13 03:04

thrag


People also ask

How do you fix RangeError maximum call stack size exceeded?

The call stack is limited in size, and when it's exceeded, the RangeError is thrown. This can happen when a deeply nested function is called, or when a lot of new variables are created. The most common way to fix this error is to reduce the number of function calls, or to limit the number of variables that are created.

What does error RangeError maximum call stack size exceeded mean?

The JavaScript RangeError: Maximum call stack size exceeded is an error that occurs when there are too many function calls, or if a function is missing a base case.

What does maximum call stack exceeded mean?

It means that somewhere in your code, you are calling a function which in turn calls another function and so forth, until you hit the call stack limit. This is almost always because of a recursive function with a base case that isn't being met.

What does error RangeError maximum call stack size exceeded see JavaScript console for details mean?

The "RangeError: Maximum call stack size exceeded" error occurs when a function is called so many times that the invocations exceed the call stack limit. To solve the error, specify a base case that has to be met to exit the recursion.


3 Answers

It's because you're comparing for object for equality rather than for reference. Change your $watch statement to this:

$scope.$watch("options.map", function (map) {
    if (map === undefined) {
        alert("map has no value");
    } else {
        alert("map is defined");
    }
});
like image 136
Roy Daniels Avatar answered Oct 18 '22 05:10

Roy Daniels


I also had this issue and found out that the objects I was comparing had circular references. (Tried JSON.stringify() which threw 'TypeError: Converting circular structure to JSON').

When I edited my object so that it didn't have circular structure, I fixed this problem and compared objects not by reference but by properties value which was what I needed.

like image 34
Marta Avatar answered Oct 18 '22 06:10

Marta


The third parameter of $watch function tells how to compare the watched object. False to reference comparing only. True to recursive equality comparing, if an object contains circular references, then over maximum stack size. For example:

var a = {ref:b};
var b = {ref:a};
$scope.$watch('b', function(){
  //code here will never called, because stack overflow when comparing the object b. 
}, true)
like image 36
lessisawesome Avatar answered Oct 18 '22 07:10

lessisawesome