Node.js RangeError: Maximum call stack size exceeded

蓝咒 提交于 2020-05-17 07:01:06

问题


I am trying to generate all possible combination of a name from my array productsDeduped and then store the generated combination as productTags array. Here is my code:

//function to generate all possible combinations
function getCombinations(array) {
    var i,
        result = [];
    for (i = 0; i < array.length; i++) {
        result.push(
            array[i],
            ...getCombinations(array.filter((_, j) => i !== j)).map(v => array[i] + " " + v)
        );
    }

    return result;
}

for (var i = 0; i < arrLength; i++) {
    var productNameStrings = getCombinations(productsDeduped[i].tags);

    productsDeduped[i].productTags = productNameStrings   
}

I get this error:

          result.push(
           ^

RangeError: Maximum call stack size exceeded
    at getCombinations (/Users/farhadam/Silobee/Inventory/categories/execute.js:1006:16)
    at Object.<anonymous> (/Users/farhadam/Silobee/Inventory/categories/execute.js:1016:30)
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)

Any idea how I can fix this; I assume its because of the high volume of request?

EDIT: I just looked deeper and when I limit my productNameStrings array of strings to only 9 elements the code works no problem; but if it has 10 or more strings in the array it starts giving me this error! Still dont know how to fix it.

来源:https://stackoverflow.com/questions/61553858/node-js-rangeerror-maximum-call-stack-size-exceeded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!