Express API Endpoint永远不会返回数据。 Sequelize

问题描述 投票:0回答:1

我有一个使用Sequelize作为ORM表达的应用程序。我有几个模型正常,但我还需要从另一个模型中提取信息以获得过滤列表。该端点控制器的模型只是帮助建议权限

  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带来任何回报。

我的逻辑有缺陷吗?

node.js express sequelize.js
1个回答
1
投票

您的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.
          })
        })
      })

  }
© www.soinside.com 2019 - 2024. All rights reserved.