【问题标题】:request-promise within get request response being duplicated?获取请求响应中的请求承诺被重复?
【发布时间】:2018-11-09 22:00:52
【问题描述】:

我的服务器中有以下路由:

//packages
const express = require('express');
const router  = express.Router();
const rp      = require('request-promise');

//date logic
var today = new Date();
var d = today.getDate();
var m = today.getMonth()+1; //January is 0!
var y = today.getFullYear();
if(d<10) {   d = '0'+d } 
if(m<10) {   m = '0'+m } 
today = y + m + d;

//needs error handling 
//retrieve specific ocr records
router.get('/odds/:bid', (req,res,next) => {

    //sports action API connection
    const actionApi = {
        url: `https://api-prod.sprtactn.co/web/v1/scoreboard/${req.params.bid}?date=${today}`,
        json: true
    }

    //home team, away team, opening odds, and closing odds API pul
    rp(actionApi)
        .then((data) => { 

    const games = data.games

        games.forEach((games) => {

            games.teams.forEach((teams, i) => {
                if (games.home_team_id == games.teams[i].id) {
                    homeTeam.push({home_team: games.teams[i].full_name}); 
                } else if (games.away_team_id == games.teams[i].id) {
                    awayTeam.push({away_team: games.teams[i].full_name}); 
                }
            })

            games.odds.forEach((odds, i) => {
                if (games.odds[i].type == "game" && games.odds[i].book_id == "15") {
                    currOdds.push({
                                    currAwayLine: games.odds[i].ml_away, 
                                    currHomeLine: games.odds[i].ml_home, 
                                    currAwaySpread: games.odds[i].spread_away, 
                                    currHomeSpread: games.odds[i].spread_home, 
                                    currAwayTotal: games.odds[i].total,
                                    currHomeTotal: games.odds[i].total,
                                    homeMlBets: games.odds[i].ml_home_public,
                                    awayMlBets: games.odds[i].ml_away_public,
                                    totalOverBets: games.odds[i].total_over_public,
                                    totalUnderBets: games.odds[i].total_under_public,
                                    spreadHomeBets: games.odds[i].spread_home_public,
                                    spreadAwayBets: games.odds[i].spread_away_public
                                })
                } else if (games.odds[i].type == "game" && games.odds[i].book_id == "30") {
                    openOdds.push({
                                    openAwayLine: games.odds[i].ml_away, 
                                    openHomeLine: games.odds[i].ml_home, 
                                    openAwaySpread: games.odds[i].spread_away, 
                                    openHomeSpread: games.odds[i].spread_home,
                                    openAwayTotal: games.odds[i].total,
                                    openHomeTotal: games.odds[i].total
                                })
                } 
            })
        })

            for (i = 0; i < homeTeam.length; i++) {
                mergRecs.push({
                    homeTeam: homeTeam[i].home_team, 
                    awayTeam: awayTeam[i].away_team,
                    currAwayLine: currOdds[i].currAwayLine,
                    currHomeLine: currOdds[i].currHomeLine,
                    openAwayLine: openOdds[i].openAwayLine,
                    openHomeLine: openOdds[i].openHomeLine,
                    currAwaySpread: currOdds[i].currAwaySpread,
                    currHomeSpread: currOdds[i].currHomeSpread,
                    openAwaySpread: openOdds[i].openAwaySpread,
                    openHomeSpread: openOdds[i].openHomeSpread,
                    currAwayTotal: currOdds[i].currAwayTotal,
                    currHomeTotal: currOdds[i].currHomeTotal,
                    openAwayTotal: openOdds[i].openAwayTotal,
                    openHomeTotal: openOdds[i].openAwayTotal,
                    homeMlBets: currOdds[i].homeMlBets,
                    awayMlBets: currOdds[i].awayMlBets,
                    totalOverBets: currOdds[i].totalOverBets,
                    totalUnderBets: currOdds[i].totalUnderBets,
                    spreadHomeBets: currOdds[i].spreadHomeBets,
                    spreadAwayBets: currOdds[i].spreadAwayBets
                })

            }
            res.send(mergRecs)
    })
    .catch((err) => {
        console.log(err);
    });
})


module.exports = router; //make router exportable

带有 get 请求的 request-promise 调用外部 API。然后将来自外部 API 的请求解析为简化的有效负载。包装了 request-promise 的 get 请求然后返回这个减少的有效负载。第一次调用我的 get 请求时,它会正确返回有效负载,但是一旦您再次请求它,它会多次返回相同的有效负载。

我尝试在 get 请求中放置一个简单的响应,例如“res.send('hello world')”,并且 hello world 会返回正常的次数。但由于某种原因,我的 request-promise 的有效负载在它是在 get 请求中调用的。我似乎无法弄清楚为什么会这样。

以下是两次调用 get 请求时控制台日志的截图:

【问题讨论】:

  • 你在哪里声明 mergRecs 在你的代码中?,我不确定我是否正确理解你的问题,你能显示预期的输出,你得到什么?
  • currOdds - 也许是全球性的?当然看起来你只是不断地推,推,推到那个可怜的全局数组
  • 这就是我的想法@JaromandaX
  • 哦,是的,至少有 五个“全局变量”从未“清除”-mergRecscurrOddsopenOddshomeTeam 和 @987654330 @

标签: javascript node.js express request-promise


【解决方案1】:

您似乎在router.get('/odds/:bid', () =&gt; {} 之外定义mergRecsopenOddshomeTeamcurrOdds

每个请求都不断推送到该数组,这就是响应被“复制”的原因。

您需要在回调中声明这些数组。

router.get('/odds/:bid', (req,res,next) => {
    const mergeRecs = [];
    const currOdds = [];
    const openOdds = [];
    const homeTeam = [];
    /* ... */
});

const mergRecs = [];
function badMiddleware() {
  // mergRecs needs to be declared here
  mergRecs.push('yes');
  console.log(mergRecs);
}

badMiddleware(); // 1 yes
badMiddleware(); // 2 yes
badMiddleware(); // 3 yes

这只是你的问题的开始。看起来您可能正在访问 currOddsopenOdds 的未定义索引,因为我怀疑这两个数组的长度与 homeTeam 相同。如果他们这样做了,那么您似乎很幸运。

【讨论】:

  • 不妨看看currOddsopenOddshomeTeamawayTeam也是怎么用的!!!
  • 是的,我看到很多对这些变量的访问,可能不存在。
  • 谢谢!我最初在服务器文件中定义了我的变量,但我从未将它们移动到我的路由中。现在完美运行!
  • 检查我更新的答案,您的代码将来可能会失败。我不知道该 API 的确切输出,但它对我来说并不好。
  • 您的意思是关于“badModdleware”部分吗?
猜你喜欢
  • 2018-08-11
  • 2021-06-21
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
  • 2018-01-09
相关资源
最近更新 更多