I'm trying to pull the data from a url, but am unsure as to what I'm doing wrong. When I console.log the returned result.data, I get a Promise, but when I console.log result.data inside of my function, I get all of the data. Here is my code:
async function getTeams() {
const result = await axios({
method: "get",
url: 'http://api.sportradar.us/ncaafb-t1/teams/FBS/2018/REG/standings.json?api_key=sg8pf7bdjt5u8ueedttyytwx',
});
console.log(result.data);
return result.data;
}
console.log(getTeams());
I'm not sure what I should be doing differently to get a completed promise.
EDIT: I needed to use a getTeams().then() with callbacks to work better. However, now I am running into an issue of storing the data inside the .then() that I can access into global variables. My code now looks like:
async function getTeams() {
const result = await axios({
method: "get",
url: 'http://api.sportradar.us/ncaafb-t1/teams/FBS/2018/REG/standings.json?api_key=sg8pf7bdjt5u8ueedttyytwx',
});
return result.data;
}
let teams = [];
getTeams().then(data => {
let acc = data.division.conferences[0];
for (let i = 0; i < acc.teams.length; i++) {
teams.push(acc.teams[i]);
}
})