Async.timesLimit () will not accept a valid callback function

PROBLEM

I am working with an API using the async library for node. I stumbled upon an obstacle that I simply cannot get around.

I am modifying an object in a database via a RESTful API. The command I'm using is called ModifyObject

and it works. I am doing another function that allows me to edit multiple objects at the same time asynchronously. But I don't want to hit my server with 100 requests right away, so I use async.timesLimit()

. You can find the documentation for this feature here . Here is my utility file shared.js

:

var async = require('async');

exports.ModifyObject = function (objectId, data, callback) {
    setup.api()
        .json()
        .patch('/Object(' + objectId + ')')
        .header("X-ApiKey", setup.apiKey())
        .send(data)
        .end(function (err, res, body) {
          if(err) throw err;
          callback(res);
    });
};

exports.ModifyMultipleObjects = function (arrayOfObjectIds, data, callback) {
    var failedArray = [];
    async.timesLimit(arrayOfObjectIds.length, 3, function (n, next) {
        exports.ModifyObject(arrayOfObjectIds[n], data, function (response) {
            if(response.statusCode != 204) failedArray.push("Failed to modify object: " + arrayOfObjectIds[n]);
            next(null);
        });
    }, function (err, failedArray) {
        if(err) throw err;
        callback(failedArray);
    });
};

      

data

- JSON object.

So, I create multiple objects, then I call ModifyMultipleObjects

into an array of my ids, but I get TypeError: undefined is not a function

thrown at me. This is how I call it in the mocha test:

var shared = require('../shared.js');

describe('test', function () {
    it('modify multiple objects', function (done) {
        var modData = {
            "propA": 100,
            "propB": 200
        };
        shared.ModifyMultipleObjects(objArray, modData, function (errArr) {
            if(errArr.length > 0) throw new Error(errArr);
            done();
        });
    });
});

      

Valid objArray

. I have checked this several times.

Stacktrace error

Uncaught TypeError: Cannot read property undefined is not a function
at Object.exports.ModifyMultipleObjects(C:blahblahblah\shared.js:1374:8)
at Context.<anonymous> (C:\blahblahblah\general\modify_multiple_ojbects.js:46:10)

      

String from modify_multiple_objects.js

:

shared.ModifyMultipleObjects(objArray, modData, function (errArr) {

String from shared.js

:

async.timesLimit(arrayOfObjectIds.length, 3, function (n, next) {

+3


source to share


1 answer


I rewrote both functions with async.timesLimit()

and async.eachLimit()

(as suggested by @Bergi).

eachLimit()

:

    async.eachLimit(objectIds, 5, function (item, next) {
        exports.ModifyObject(item, data, function (res) {
            if (res.statusCode != 204) throw new Error("failed to modify object " + item);
            next();
        }, function (err) {
            callback();
        });
    });

      

timesLimit()

:



    async.timesLimit(objectIds.length, 5, function (n, next) {
        exports.ModifyObject(objectIds[n], data, function (res) {
            if(res.statusCode != 204) throw new Error("failed to modify object " + objectIds[n]);
            next();
        });
    }, function (err) {
        callback();
    });

      

The function is timesLimit()

throwing the same error Uncaught TypeError: undefined is not a function

as stated in the question.

However, it eachLimit()

works fine. However, I don't understand why. As far as I can tell, timesLimit()

it is written correctly and the two functions should be identical.

+1


source







All Articles