I have a table called categories which should hold the categories of articles available on my site. I want to have a parent and child system in the table, where a category with no parent has the value of 0 for its parent column. A category that has a parent will hold the id of its parent category in its parent column.
To query it I have created this function:
function categoryTree(table, parent = 0) {
var categTreeArr = [];
return new Promise(resolve => {
dbCon.promise().execute("select * from " + table + " where parent = " + parent, []).then(function (err, result, fields) {
result.forEach(e => {
var categTreeArrNext = categoryTree(table, e.id).then((value) => {
categTreeArr.push([e.img, e.name, e.slug, value]);
});
});
resolve(categTreeArr);
})
})
}
On my index page, I have used this code to call the function:
categArr = myModule.categoryTree("course_category", 0).then(value => {
setValue(row, response, value)
console.log(value)
});
This gives me an UnhandledPromiseRejectionWarning. Please Help.
PS: I am new to NodeJS. If anything about this question is wrong, please don't flag it. If the question is unclear, I will clarify it in the comment.