node.js - Express API Endpoint 从不返回数据。 Sequelize

标签 node.js express sequelize.js

我有一个使用 Sequelize 作为 ORM 的应用程序。我有几个模型运行良好,但我还需要从另一个模型中提取信息以获得过滤列表。此端点 Controller 的模型只是协助建议实体

  fiveFurthestInsiders(req, res) {
    let targetArea = req.params.areaId; 
    //Set the area we wish to set a match for...
    let gender = req.params.gender;
    //matches must be of the same gender.
    let cty = req.params.country; 
    //and from the same country
    let lang = req.params.language; 
    //must also speak the same language
    let distances = []; 
    //holds the distance calculations. (calculated for each request.)
    let returnUsers = []; 
    //holds the suggestions to return to the F.E.
    return Area.findAll() 
       //get all the areas and Geo Codes
      .then(AreaList => {
        AreaList.forEach(x => { 
        //foreach Area of the Area List
          distances.push({ 
            //add to the distance array
            target: targetArea, 
            //area being matched
            source: x, 
            //area for this index
            distance: areaController.calcDist(targetArea, x),
            //distance from target to x (KM).
            country: areaController.determineCountry(targetArea) 
            //determine if country for target.
          });
        });
      }).then(_ => {
        distances.sort((x, y) => { 
           //sort the array Descending.
          return x.distance - y.distance;
        }).filter(x => x.country === cty);
           //filter out other countries
      }).then(x => {
        if (distances.indexOf(x) < 10) { 
           //cut out all but the 10 furthest areas for limiting purposes
          return x;
        }
      }).then(x => {
        distances.forEach(y => { 
          //foreach item in the distance array.
          Outsider.findAll({
            //find the first two people
            where: {
              areaCode: y.target,
              //that are in the far area
              gender: gender,
              // are of the same gender
              language: lang,
              //speak the same language
              country: cty
              //and are in the same country
            },
            limit: 2
          }).then(insiders => {
            returnUsers.push(insiders);
            //add the people from the query to the return list.
          })
        })
      })
    returnUsers = returnUsers.splice(0, 10);
    //Cut down the array again as some areas might 
    //have had more or less people to add.
    res.status(200).send(returnUsers);
    //send the array of people.
  }

一切似乎都在工作,但它永远不会返回任何东西给 Postman。

我的逻辑有问题吗?

最佳答案

您的 Sequelize 数据库查询是 Promise ,这意味着它是一个异步过程。考虑将您的 res.send 移至最后一个,然后如下所示

  fiveFurthestInsiders(req, res) {
    let targetArea = req.params.areaId; 
    //Set the area we wish to set a match for...
    let gender = req.params.gender;
    //matches must be of the same gender.
    let cty = req.params.country; 
    //and from the same country
    let lang = req.params.language; 
    //must also speak the same language
    let distances = []; 
    //holds the distance calculations. (calculated for each request.)
    let returnUsers = []; 
    //holds the suggestions to return to the F.E.
    return Area.findAll() 
       //get all the areas and Geo Codes
      .then(AreaList => {
        AreaList.forEach(x => { 
        //foreach Area of the Area List
          distances.push({ 
            //add to the distance array
            target: targetArea, 
            //area being matched
            source: x, 
            //area for this index
            distance: areaController.calcDist(targetArea, x),
            //distance from target to x (KM).
            country: areaController.determineCountry(targetArea) 
            //determine if country for target.
          });
        });
      }).then(_ => {
        distances.sort((x, y) => { 
           //sort the array Descending.
          return x.distance - y.distance;
        }).filter(x => x.country === cty);
           //filter out other countries
      }).then(x => {
        if (distances.indexOf(x) < 10) { 
           //cut out all but the 10 furthest areas for limiting purposes
          return x;
        }
      }).then(x => {
        distances.forEach(y => { 
          //foreach item in the distance array.
          Outsider.findAll({
            //find the first two people
            where: {
              areaCode: y.target,
              //that are in the far area
              gender: gender,
              // are of the same gender
              language: lang,
              //speak the same language
              country: cty
              //and are in the same country
            },
            limit: 2
          }).then(insiders => {
            returnUsers.push(insiders);
            //add the people from the query to the return list.
            returnUsers = returnUsers.splice(0, 10);
            //Cut down the array again as some areas might 
            //have had more or less people to add.
            res.status(200).send(returnUsers);
            //send the array of people.
          })
        })
      })

  }

关于node.js - Express API Endpoint 从不返回数据。 Sequelize ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48083561/

相关文章:

javascript - CSSLint : How to save log from console log into file

javascript - Node.js http.request keepAlive

node.js - req.form 不是实例化的多方表单对象

node.js - 使用 Typescript 在 Node 中将 Sequelize 模型作为属性传递

Node.js 命令行脚本有时不会终止

node.js - react 样板 npm 安装错误

express - Apollo Server Express : Request entity too large

node.js - 如何使用 Sequelize 在 postgres 中保存列类型范围内的值?

javascript - Get请求返回html代码而不是真实数据

node.js - async/await 函数未按正确顺序执行