fredcrs fredcrs - 3 months ago 3
Node.js Question

Herarchy query using seuelize / nodejs

I am trying to load a hierarchy in my database. I have a column with parentId in my table so every row can have a parent. But I am having problems using recursion and promises.

function read (options) {
return serviceItemAttributeModel.findOne({
id_organization: options.idOrganization
.then((attribute) => {
if (attribute) {
return loadChildren(attribute, attribute);
} else {
return attribute;

function loadChildren (root, attribute) {
return serviceItemAttributeModel.findAll({
where: {
.then((attributes) => {
if (!attributes) {
return root;
} else {
attribute.serviceItemAttributes = [];
attributes.forEach(function (each) {
return loadChildren(root, each);

So, I call read that calls loadChildren to recursively try to load all entities (by looking children of an entity) and I get an undefined value. Any ideas?

I am also getting an error on console: a promise was created in a handler but was not returned from it


You messing up with async calls and returns. You can convert both function to async, and pass through result structure to be updated. Example:

function read(...) {
  return new Promise(function (accept, reject) {
    // You code goes here, but instead of return
// ...
read(...).then(function(resultData) { ... });