async.eachOfSeries中collectionsDone非函数错误解决求助
问题分析与解决方案
我一眼就揪出了问题的核心——你把async.eachOfSeries迭代器函数的参数顺序搞混了!
错误根源
async.eachOfSeries的迭代器函数正确参数顺序是:
function(item, key/index, callback)
但你的代码里写的是:
function(collection, collectionsDone)
这里的第二个参数collectionsDone其实是当前迭代的键(或索引值),根本不是异步回调函数!这就导致你调用collectionsDone()时,本质是在调用一个数字/字符串,自然会抛出collectionsDone is not a function的错误。
你后来尝试用IIFE包裹代码,完全是找错了方向——问题和作用域无关,纯粹是参数顺序搞错了。
修正后的代码
我帮你调整了参数顺序,去掉了多余的IIFE,同时规范了异步回调的错误传递逻辑:
async.eachOfSeries(shop.collections, function(collection, key, collectionsDone) { if (!collection.countrId) { // 该分类尚未同步到countr,创建新分类 createCategory(collection[language].name, access_token, function(err, category) { if (category) { collection.categoryId = category._id; } return collectionsDone(err); // 传递错误,让async自动处理错误分支 }); } else { // 分类已同步,检查是否需要更新 var link = 'categories/' + collection.categoryId; externalService.callApi({url: link}, null, access_token, function(err, category) { console.log("err", err, "category", category); if (err?.error == "not_found") { // 分类已丢失,重新创建 createCategory(collection[language].name, access_token, function(err, category) { if (category) { collection.categoryId = category._id; } return collectionsDone(err); }); } else { if (category.name != collection[language].name) { // 名称不一致,执行更新操作 var data = { _id: category._id, name: collection[language].name, color: category.color, visible: category.visible }; var url = 'categories/' + category._id; externalService.callApi({ url: url, method: "PATCH" }, data, access_token, function(err) { return collectionsDone(err); }); } else { // 无需更新,直接结束当前迭代 return collectionsDone(); } } }); } }, function(err) { // eachOfSeries完成后的最终回调 if (err) { return callback({code: 500, msg: err}); } shop.save(function(err, result) { console.log("shop save", err, result); if (err) { return callback({code: 500, msg: err}); } return callback(null, result); }); });
额外优化说明
- 所有
collectionsDone调用都传递了err参数,遵循async库的规范——一旦异步操作出错,会直接跳转到最终的错误处理分支,避免无效迭代。 - 用可选链
err?.error替代err.error,防止err为null时触发额外报错。 - 明确了最终回调的
err参数,让错误处理逻辑更清晰。
内容的提问来源于stack exchange,提问作者NVO
相关产品推荐
相关产品推荐

