I have the following function that is used to call an API.
var unirest = require("unirest");
const fetchUpcomingBySport = async(id) => {
var req = unirest("GET", "https://bet365-sports-odds.p.rapidapi.com/v1/bet365/upcoming");
req.query({
"sport_id": id
});
req.headers({
"x-rapidapi-host": "bet365-sports-odds.p.rapidapi.com",
"x-rapidapi-key": "API-KEY",
"useQueryString": true
});
req.end(function (res) {
if (res.error) throw new Error(res.error);
return(res.body.results);
});
}
module.exports = fetchUpcomingBySport;
The API call is successful. I can log the 'res.body.results' fine, however the returned value from my call let fixtures = await fetchUpcomingBySport(12)
is undefined. I believe that I have to return from outside of the res.end callback like so:
var unirest = require("unirest");
const fetchUpcomingBySport = async(id) => {
var req = unirest("GET", "https://bet365-sports-odds.p.rapidapi.com/v1/bet365/upcoming");
let results;
req.query({
"sport_id": id
});
req.headers({
"x-rapidapi-host": "bet365-sports-odds.p.rapidapi.com",
"x-rapidapi-key": "API KEY",
"useQueryString": true
});
req.end(function (res) {
if (res.error) throw new Error(res.error);
results = res.body.results;
return;
});
return results;
}
module.exports = fetchUpcomingBySport;
however when I create a variable 'results' in the beginning of the fetchUpcomingBySport, but when I set the variable = 'res.body.results' it still returns as undefined.
Any ideas?