I'm trying to retrieve a list of commits within a repository using the Github API. After retrieving this list, I want to assign it to a list variable that I defined.
The reason why I want to do this is so I can extract information about the pull request, file(s) the commit is associated with, and the number of lines added/deleted from the file(s). I have the following code so far:
var listOfCommits = [];
const axios = require('axios');
axios.get(githubAPI).then(function (response) {
listOfCommits = response['data'];
console.log("in then part");
console.log(listOfCommits[0]);
}).catch(function (error) {
console.log("Error: something wrong happened");
}).finally(function () {
console.log("hopefully this is done right...");
});
console.log("outside of the axios method");
console.log(listOfCommits[0]);
console.log("last debug");
I'm using Webstorm, so when I run the file using "node file.js" I get the following output:
outside of the axios method
undefined
last debug
in then part
[stuff retrieved from the repository]
hopefully this is done right...
The variable githubAPI
is a string that stores the URL needed for the API to retrieve the necessary information. The goal of this code is to assign the information retrieved to the variables listOfCommits
. Inside the axios.get()
method, listOfCommits
is defined, but when I try printing it out again after the method, it's undefined
.
I know that it has something to do with the fact that axios.get()
returns a promise, but I'm not sure how to fix this as I'm not too familiar with Javascript. Any help would be much appreciated. Thanks!