Reputation: 764
I've created the following method to aid me in finding an item and updating another object attribute with certain value like this:
mergeAinBbyAttr: function(a, b, attr, val, cb) {
async.forEach(a, function(itemA, cb) {
async.forEach(b, function(itemB, cb) {
if (itemA[attr] == itemB[attr]) {
itemB[val] = itemA[val];
}
cb(null, itemB);
},
function(e, result) {
cb(e, result);
});
},
function(e, result) {
cb(e, result);
});
}
The problem is that I get:
TypeError: cb is not a function
in the last cb(e, result);
. I have to admit I am a bit brain dead an the answer must be dead simple.
Thanks!
Upvotes: 1
Views: 5146
Reputation: 70125
There's no guarantee that cb
will be a function. Someone can pass whatever they want or pass nothing at all (in which case cb
will be undefined
).
If you believe that you are code that invokes this function is such that cb
is guaranteed to be a function, then maybe add an assert(typeof cb === 'function');
at the start of mergeAinBbyAttr
. (You may need to add a const assert = require('assert');
at the top of your file.)
If this is library code that someone else can call or if you are otherwise not guaranteed to receive a function for cb
, add some type checking:
if (typeof cb !== 'function') {
// Do something here: Throw an error, set `cb` to a no-op function, whatever
}
Upvotes: 3