【问题标题】:Express API Endpoint never returns data. SequelizeExpress API Endpoint 从不返回数据。续集
【发布时间】:2018-01-04 03:05:08
【问题描述】:

我有一个使用 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】:

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

    【讨论】:

    • 好吧,这是有道理的。我需要更好地阅读代码。
    猜你喜欢
    • 2019-06-20
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    相关资源
    最近更新 更多