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

时间:2018-01-03 19:00:57

标签: node.js express sequelize.js

我有一个使用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带来任何回报。

我的逻辑是否有缺陷?

1 个答案:

答案 0 :(得分: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.
          })
        })
      })

  }