首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >get请求响应中的request-promise是否重复?

get请求响应中的request-promise是否重复?
EN

Stack Overflow用户
提问于 2018-05-31 08:18:02
回答 1查看 156关注 0票数 0

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

代码语言:javascript
复制
//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会被返回正常的次数。但是由于某些原因,当我的请求-promise在get请求中被调用时,它的有效负载是重复的。我似乎不明白为什么会发生这种情况。“

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-31 08:33:37

看起来你在router.get('/odds/:bid', () => {}之外定义了mergRecsopenOddshomeTeamcurrOdds

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

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

代码语言:javascript
复制
router.get('/odds/:bid', (req,res,next) => {
    const mergeRecs = [];
    const currOdds = [];
    const openOdds = [];
    const homeTeam = [];
    /* ... */
});

代码语言:javascript
复制
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 & openOdds的未定义索引,因为我怀疑这两个数组的长度是否与homeTeam相同。如果他们这样做了,看起来你是非常幸运的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50614989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档