javascriptnode.jsmongodbcronnode-cron

How to fix codeblock blocking execution


async function getMatchesByFixtureId(id){

  let count = 0;
  let request = true;
  let response;

  const options = {
    method: 'GET',
    url: 'https://api-football-v1.p.rapidapi.com/v3/fixtures',
    params: {id: id},
    headers: {
      'x-rapidapi-key': API_KEY,
      'x-rapidapi-host': 'api-football-v1.p.rapidapi.com'
    }
  };

  async function innerFunction(){
    
    
      if(request === true){

        response = await axios.request(options);
        const match = response.data.response;
        console.log(match);
        let status;
        console.log(1);


              if(["CANC", "FT", "AET", "PEN", "PST", "AWD", "WO"].includes(match.fixture.status.short)){
                status = "FT";
                if(count === 4){
                  request = false;
                }
                count++;
              }
              
              else if(["TBD", "NS"].includes(match.fixture.status.short)){
                status = "SC";
                count = 0;
              }
              else if(["HT", "LIVE", "1H", "2H", "ET", "BT", "P"].includes(match.fixture.status.short)){
                status="LIVE";
                count = 0;
              }
  
          console.log(2);
          
  
        const fixture = {
           Bunch of data
        }
  
        console.log(3);
  
        
        await client.db("db").collection("matches").replaceOne(fixture);

      }

      else{
        return;
      }

  }

  const task = cron.schedule("* * * * *", async() => {
    
    await innerFunction();

  }, {timezone: "UTC"});
  task.start();

  setTimeout(task.stop, 8.1e+6)

}

So basically the getMatchesByFixtureId() above is called by another function which schedules the execution of getMatchesByFixtureId() and getMatchesByFixtureId() requests from api-football a live football match with the matching fixture id every minute.

The problem is that the code between console.log(1) and console.log(2) blocks the execution of the code bellow and when I comment it out the code between console.log(2) and console.log(3) blocks the code below if anyone is wondering yes the api request work fine I receive the right data


Solution

  • The problem was that the data I received was in an array so I had to select position 0 of the array. const match = response.data.response; -> const match = response.data.response[0];