Question

Redis gives ECONNREFUSED halfway through an API call

Posted November 10, 2021 70 views
Node.jsRedis

Hi!

I’m working with an API and I send out 10 requests each a few seconds, then pulls the responses, merges the JSON, then sends it to the redis client. halfway through the API calls redis gives the following error


Error: Redis connection to <server_ip>:6379 failed - connect ECONNREFUSED <server_ip>:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14) 

relevant node.js code


async function getSeasonsForCompetitions(competitions) {
        let totalJSON = []
        try {
                for (let i = 0; i < competitions.length; i++) {
                        await delay(1000)
//HALFWAY THROUGH THIS FUNCTION THROWS THE ERROR
                        let season = await getJSON(`https://api.sportradar.us/soccer/trial/v4/en/seasons/sr:season:${competitions[i]}/schedules.json?api_key=${process.env.API_KEY_SOCCER}`)

                        console.log(`${competitions.length - i } remain`)
                        totalJSON.push(season)
                }
                return totalJSON
        } catch(e) {
                console.log("error", e)
                return totalJSON
        }
}


async function setSchedule() {
        let soccerJSON = await getScheduleSoccer()
        console.log("got soccer object")
        let nbaJSON = await getScheduleNBA()
        console.log("got NBA object")
        let nflJSON = await getScheduleNFL()
        console.log("got nfl object")
        let nhlJSON = await getScheduleNHL()
        console.log("got nhl object")
        let mlbJSON = await getScheduleMLB()
        console.log("got mlb object")
        let f1JSON  = await getScheduleF1()
        console.log("got f1 object")
        let nbaDate = moment().format('LL') + "-NBA"
        let nflDate = moment().format('LL') + "-NFL"
        let nhlDate = moment().format('LL') + "-NHL"
        let mlbDate = moment().format('LL') + "-MLB"
        let f1Date  = moment().format('LL') + "-F1"
        let soccerDate = moment().format('LL') + "-Soccer"
        let redisArr = [
                nbaDate,
                JSON.stringify(nbaJSON),
                nflDate,
                JSON.stringify(nflJSON),
                nhlDate,
                JSON.stringify(nhlJSON),
                mlbDate,
                JSON.stringify(mlbJSON),
                f1Date,
                JSON.stringify(f1JSON),
                soccerDate,
                JSON.stringify(soccerJSON)
        ]
        console.log("writing all objects to db")
        redisClient.mset(redisArr, (err, reply) => {
                if (err) {
                        console.log(err)
                } else {

                        setLatestJSONKeys().then((obj) => {
                                process.exit();
                        })
                }
        })
}
Submit an answer

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!