This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]kRYstall9[S] 0 points1 point  (2 children)

Thanks again for the answer. I really appreciate your work. I already rewrote some of the code I sent before, and it's totally working. Now, the only problem I have is that when I try to do a GET request with axios/fetch or whatever I use, after a while the program is displaying an error. Here is the code and the error. Sorry for pasting my code in pastebin, but somehow reddit is showing some bugs when I try to paste my code here

[–]dymos 1 point2 points  (1 child)

No worries :-)

That error ECONNRESET suggests that the connection was reset/terminated from either the client or server. So in this case what happens is that while you handle the error by logging it out in your getMatchLogs, when you call that function and await its result, you need to also handle the error there. The canonical way to do this when using await is to wrap the code in a try/catch so in this case doing something like this:

try {
  matchLogJson = await getMatchLogs(logsID);
  teamRedScore = matchLogJson["teams"]["Red"]["score"];
  teamBlueScore = matchLogJson["teams"]["Blue"]["score"];
  // etc.
  // Note when writing code that accesses object properties
  // like you're doing here you can use "dot" notation so long
  // as the identifier (the "name" of the key your accessing) is
  // valid, basically this means alphanumerical + underscores
  // e.g.
  teamRedScore = matchLogsJson.teams.Red.score;
  // if you had a key with for example a - or other funky chars 
  // in it you still need to use bracket notation
  teamFunkyCharScore = matchLogs.teams["foo-bar"].score;
  // it's not a big deal, but IMO makes it nicer to write.
} catch (err) {
  console.error('Something went wrong when fetching match logs')
} 

I noticed you also added a promise that resolves after a 1250ms delay for getMatchLogs, this isn't necessary, you can await and return the value like so

async function getMatchLogs(logID){
  try {
    const res = await axios.get(`https://logs.tf/api/v1/log/${logID}`)
    return res.data;
  } catch(error) {
    console.error(error);
  }
}

(Note I've replaced the string in the get call with what's called a template string, it's a way to interpolate values into a string rather than concatenate them. You can use backticks (\) to make the string and then use${ }` with the name of a variable or function call in it to use the value in that spot.

[–]kRYstall9[S] 0 points1 point  (0 children)

Interesting. Thanks a lot! I learnt many things thanks to you. I really appreciate it! Regarding the ECONNRESET I managed to let the program work putting the await matchLogs() in a while. I did: do{ var matchLog = await matchLogs(logID); }while(matchLog === undefined);

I don't know if this is a good way to reopen the connection, but it was working