get请求中的请求 - 承诺是否重复?

时间:2018-05-31 00:18:02

标签: javascript node.js express request-promise

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

//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请求然后返回此减少的有效负载。第一次调用我的get请求时,它会正确返回有效负载,但是一旦再次请求它,它会多次返回相同的有效负载。

我尝试在get请求中添加一个简单的响应,例如&#34; res.send(&#39; hello world&#39;),并且hello world返回正常的次数。但由于某种原因,我的请求 - 承诺的有效负载在get请求中被调用时会重复。我似乎无法弄清楚为什么会这样。

以下是两次调用get请求时控制台日志的屏幕截图: enter image description here

1 个答案:

答案 0 :(得分:2)

您似乎正在定义mergRecsopenOddshomeTeam&amp; currOdds以外的router.get('/odds/:bid', () => {}

每个请求都会继续推送到该数组,这就是响应被“重复”的原因。

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

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

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

相关问题