node.js - 使用 sequelize/node js 进行层次结构查询

标签 node.js recursion promise sequelize.js

我正在尝试在我的数据库中加载层次结构。我的表中有一个带有 parentId 的列,因此每一行都可以有一个父级。但是我在使用递归和 promise 时遇到了问题。

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

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

因此,我调用 读取 调用 loadChildren 以递归尝试加载所有实体(通过查看实体的子项),我得到一个未定义的值。有任何想法吗?

我也在控制台上收到一个错误:在处理程序中创建了一个 promise ,但没有从中返回。

编辑:

如果在 Nosyara 帮助之后这个解决方案出现了。谢谢!:
function read (options) {
  return serviceItemAttributeModel.findOne({
    where: {
      id: options.attributeId,
      id_organization: options.idOrganization
    }
  })
  .then((attribute) => {
    if (!attribute) {
      return new Promise(function (resolve, reject) {
        resolve(attribute);
      });
    } else {
      return new Promise(function (resolve, reject) {
        attribute.queryCount = 1;
        resolve(attribute);
      })
      .then((attribute) => loadChildren(attribute, attribute));
    }
  });
}

function loadChildren (root, attribute) {
  return new Promise(function (resolve, reject) {
    return serviceItemAttributeModel.findAll({
      where: {
        id_parent: attribute.id
      }
    })
    .then((attributes) => {
      attributes.length = attributes.length || 0;
      root.queryCount = root.queryCount - 1 + attributes.length;
      if (root.queryCount === 0) {
        resolve(root);
      } else if (root.queryCount > 10) {
        let error = new Error('Service attribute hierarchy cant have more then 10 levels');
        error.statusCode = 500;
        reject(error);
      } else {
        attribute.serviceItemAttributes = [];
        attributes.forEach(function (each) {
          attribute.serviceItemAttributes.push(each);
          return loadChildren(root, each).then(() => {
            resolve(root);
          });
        });
      }
    });
  });
}

最佳答案

你搞砸了异步调用和返回。您可以将两个函数都转换为异步,并通过结果结构进行更新。例子:

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

Here 是 Promise 递归的例子。

关于node.js - 使用 sequelize/node js 进行层次结构查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38514078/

相关文章:

javascript - module.exports 函数在另一个文件的 require 之后未定义

node.js - 使用 axios 响应 native 请求

node.js - ElasticBeanStalk 未部署我的 Node 应用程序

java - 如何获取项目之间的所有路径

javascript - 使用参数执行 Promise.all

javascript - 为什么 NodeJS 不将 Promise 用于 readFile API?

javascript - UnhandledPromiseRejectionWarning,但我正在处理错误?

c - 二叉搜索树指针问题

javascript - trim 对象树上的叶子

javascript - Angular $q 绑定(bind) this