javascript - async.timesLimit() will not accept a valid callback function -
problem
i working api using async library node. i've hit obstacle can't seem around.
i modifying object in database through restful api. command i'm using called modifyobject
, works. making function allows me edit multiple objects @ once asynchronously. but, don't want hit server 100 requests @ once, i'm using async.timeslimit()
. can find the documentation function here. here shared.js
utility function file:
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 modify object: " + arrayofobjectids[n]); next(null); }); }, function (err, failedarray) { if(err) throw err; callback(failedarray); }); };
data
json object.
so, create multiple objects, call modifymultipleobjects
on array of ids, typeerror: undefined not function
thrown @ me. here how i'm calling in 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(); }); }); });
the objarray
valid. have checked multiple times.
error stacktrace
uncaught typeerror: cannot read property undefined not function @ object.exports.modifymultipleobjects(c:blahblahblah\shared.js:1374:8) @ context.<anonymous> (c:\blahblahblah\general\modify_multiple_ojbects.js:46:10)
the line modify_multiple_objects.js
is:
shared.modifymultipleobjects(objarray, moddata, function (errarr) {
the line shared.js
is:
async.timeslimit(arrayofobjectids.length, 3, function (n, next) {
i rewrote both functions using async.timeslimit()
, async.eachlimit()
(as suggested @bergi).
eachlimit()
:
async.eachlimit(objectids, 5, function (item, next) { exports.modifyobject(item, data, function (res) { if (res.statuscode != 204) throw new error("failed 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 modify object " + objectids[n]); next(); }); }, function (err) { callback(); });
the timeslimit()
function throws same uncaught typeerror: undefined not function
error stated in question.
however, eachlimit()
works perfectly. don't understand why though. far can tell, timeslimit()
written correctly, , these 2 functions should identical.
Comments
Post a Comment