获取请求响应中的请求承诺被复制?

request-promise within get request response being duplicated?

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

//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 请求的请求承诺调用外部 API。然后将来自外部 API 的请求解析为简化的有效负载。 get 请求将 request-promise 包装在 returns 这个减少的有效负载中。第一次调用我的 get 请求它 returns 正确的有效负载,但是一旦你再次请求它 returns 相同的有效负载多次。

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

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

您似乎在 router.get('/odds/:bid', () => {}

之外定义 mergRecsopenOddshomeTeamcurrOdds

并且每个请求都不断推送到该数组,这就是响应 "duplicated".

的原因

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

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 相同。如果他们这样做了,就好像你很幸运。