简体   繁体   中英

Nodejs RangeError Maximum Call stack exceeded

There are more than 2000 objects in the rows array which need to be processed but got error Maximum call Stack exceeded.Callback function are manipulating database. I tried to use

setTimeout

that is working but making the execution slow. Is there any other method to fix it.

var updateRowsStatus = function (req, rows, next) {

    if (rows.length == 0) {
        return next();
    }

    var batchRows = rows.splice(0, 20);
    var count = 0;


    batchRows.forEach(function (row) {

       // other function 

        updateSubEntity(req, row, 'rows', function (err, response) {
            if (err)throw err;

            if (++count == batchRows.length) {

               updateRowsStatus(req, rows, next);

            }
        });
    });
};

Someone posted this solution but removed. So i'm posting again his solution. Thanks to him.

    var count = 0;
    var length = rows.length;

    while (rows.length > 0) {

        console.log('rows -', rows.length);

        var batchRows = rows.splice(0, 20);

        batchRows.forEach(function (row) {
            updateSubEntity(req, row, 'rows', function (err, response) {
                if (err)throw err;

                if (++count == length) {

                    return next();
                }

            });
        });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM