From d5af39ca5df8d30e649c0727cfb6c50907636a4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 22 Nov 2018 18:21:43 -0500 Subject: [PATCH] make 1 call for parentCids --- src/categories/data.js | 3 +-- src/categories/index.js | 28 +++++++++++++++------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/categories/data.js b/src/categories/data.js index 96ed3214a5..d252953eef 100644 --- a/src/categories/data.js +++ b/src/categories/data.js @@ -16,10 +16,9 @@ module.exports = function (Categories) { return setImmediate(callback, null, []); } - var keys = cids.map(cid => 'category:' + cid); - async.waterfall([ function (next) { + const keys = cids.map(cid => 'category:' + cid); if (fields.length) { db.getObjectsFields(keys, fields, next); } else { diff --git a/src/categories/index.js b/src/categories/index.js index efb3cbad70..eebbebe541 100644 --- a/src/categories/index.js +++ b/src/categories/index.js @@ -235,19 +235,21 @@ Categories.getParentsAndChildren = function (categoryData, uid, callback) { }; Categories.getChildren = function (cids, uid, callback) { - var categories = cids.map(cid => ({ cid: cid })); - - async.each(categories, function (category, next) { - Categories.getCategoryField(category.cid, 'parentCid', function (err, parentCid) { - if (err) { - return next(err); - } - category.parentCid = parentCid; - getChildrenRecursive(category, uid, next); - }); - }, function (err) { - callback(err, categories.map(c => c && c.children)); - }); + var categories; + async.waterfall([ + function (next) { + Categories.getCategoriesFields(cids, ['parentCid'], next); + }, + function (categoryData, next) { + categories = categoryData.map((category, index) => ({ cid: cids[index], parentCid: category.parentCid })); + async.each(categories, function (category, next) { + getChildrenRecursive(category, uid, next); + }, next); + }, + function (next) { + next(null, categories.map(c => c && c.children)); + }, + ], callback); }; function getChildrenRecursive(category, uid, callback) {