I'm a beginner in javascript and currently experimenting with fetching data from an API and posting it to my own server (localhost).
For fetching the data, I am using axios as shown below:
async function getNCAA() {
axios
.get(`https://api.the-odds-api.com/v4/sports/${sportKey1}/scores/?daysFrom=1&apiKey=${apiKey}`)
.then((res) => {
console.log(res.data)
console.log("Remaining requests", res.headers["x-requests-remaining"]);
console.log("Used requests", res.headers["x-requests-used"]);
return res.data
})
.catch((error) => {
console.log("Error status", error.response.status);
console.log(error.response.data);
return error
});
}
Next, I need to store the fetched data.
let result = getNCAA();
Then, I attempt to send the data using express:
app.get('/', (req, res) => {
res.send(result);
});
The issue arises when the result returns a Promise object that I struggle to access.
In previous cases, I could access this data by using useState, but in this instance, it's not a React app.