From d656c65c9a925f998d8f92446ee0677835210ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sun, 10 Jun 2018 10:28:17 -0400 Subject: [PATCH] closes #6567 prevent crash if category is undefined --- src/categories.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/categories.js b/src/categories.js index ab1d277e68..1270924f0e 100644 --- a/src/categories.js +++ b/src/categories.js @@ -307,21 +307,19 @@ Categories.flattenCategories = function (allCategories, categoryData) { */ Categories.getTree = function (categories, parentCid) { var tree = []; - var i = 0; - var len = categories.length; - var category; - for (i; i < len; i += 1) { - category = categories[i]; - if (!category.hasOwnProperty('parentCid') || category.parentCid === null) { - category.parentCid = 0; - } + categories.forEach(function (category) { + if (category) { + if (!category.hasOwnProperty('parentCid') || category.parentCid === null) { + category.parentCid = 0; + } - if (parseInt(category.parentCid, 10) === parseInt(parentCid, 10)) { - tree.push(category); - category.children = Categories.getTree(categories, category.cid); + if (parseInt(category.parentCid, 10) === parseInt(parentCid, 10)) { + tree.push(category); + category.children = Categories.getTree(categories, category.cid); + } } - } + }); return tree; };