【问题标题】:Herarchy query using sequelize / nodejs使用sequelize / node js的层次结构查询
【发布时间】:2016-11-25 15:03:26
【问题描述】:

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

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);
      });
    }
  });
}

所以,我调用 read 来调用 loadChildren 以递归方式尝试加载所有实体(通过查看孩子一个实体),我得到一个未定义的值。有什么想法吗?

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

编辑:

如果这个解决方案是在 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);
          });
        });
      }
    });
  });
}

【问题讨论】:

    标签: node.js recursion promise sequelize.js


    【解决方案1】:

    你搞砸了异步调用和返回。您可以将这两个函数都转换为异步,并传递要更新的结果结构。示例:

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

    Here 是 Promise 递归的示例。

    【讨论】:

      猜你喜欢
      • 2019-09-05
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 2020-04-17
      • 2015-10-15
      • 1970-01-01
      相关资源
      最近更新 更多