【问题标题】:Sequelize, Nested Findall and merge all find all resultSequelize,嵌套 Findall 并合并所有查找所有结果
【发布时间】:2020-09-01 02:02:23
【问题描述】:

这是需求代码,dataJoin(像多条记录一样的对象数组)结果,有json值。这些 json 值,必须找到相关项。因此,用于语句,用于查找 json 值。收集 json 值,使用 userService 函数,并获取数据,然后将这些数据合并到父数据。它不工作。因为,在完成for循环语句之前,res.send("...")项已经执行,这是我的问题...

请任何人指导我...

谢谢

  table1Obj
  .findAll(
    {  
      include: [
        {
            model: assocaitonTableObject,
            required: false,
        }
      ],
      where: req.query,
    })
    .then((dataJoin) => {


      for(element of dataJoin ) {
        const arrayCollection = [];
        let whereConstraint = {};
        JSON.parse(element.joinedUsersList).forEach( async (ele) => { 
          arrayCollection.push(ele.userId);
        });;
        whereConstraint = {
          id : {
            [Op.in]: arrayCollection
          }
        }
        const usesrListData = userService.customfindAll(whereConstraint);
        element.userListData = usesrListData;
      }

      res.send({
        status:200,
        message:"OK",
        data:dataJoin
      });

    })
    .catch((err) => {
      res.status(500).send({
        status:500,
        message:
          err.message ,
      });
    });

【问题讨论】:

    标签: sequelize.js findall


    【解决方案1】:

    我的第一个猜测是你的userService.customfindAll 返回一个 Promise,那么你应该等待它。如果不是,请检查您是否等待每个 Promise。

    我的第二个猜测是,您不应该为 array.forEach 传递异步函数,请尝试改用 for。我不确定这是否会有所帮助,但你可以试试。

    【讨论】:

    • 你真的在等待everythink吗?您的问题看起来像是没有等待一些承诺。我知道这是多余的,但请检查一下。 PS:发布您的 userService 和 customFindAll 代码。
    • 亚历克斯,你能把你的电子邮件分享给我吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多