Changes to index.js structure for middleware
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
const axios = require('axios');
|
||||
|
||||
// Middleware function to fetch leaguees data
|
||||
const fetchleagueesData = async (req, res, next) => {
|
||||
// Middleware function to fetch leagues data
|
||||
const fetchLeaguesData = async (req, res, next) => {
|
||||
try {
|
||||
// Array of years to fetch leaguees data
|
||||
const league_ids = [2021]; /* Readd , 2002, 2014, 2019, 2015, 2013 */
|
||||
// Array of years to fetch leagues data
|
||||
const league_ids = [2021]; /* Add more league IDs if needed */
|
||||
|
||||
// Array to store all leaguees data
|
||||
// Array to store all leagues data
|
||||
let allLeagues = [];
|
||||
|
||||
// Loop through each year and fetch leaguees data
|
||||
// Loop through each year and fetch leagues data
|
||||
for (const league_id of league_ids) {
|
||||
const response = await axios({
|
||||
url: `http://api.football-data.org/v4/competitions/${league_id}/standings?season`, /* Resinsert ${league_id} for 2021 */
|
||||
url: `http://api.football-data.org/v4/competitions/${league_id}/standings?season`,
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'X-Auth-Token': '0aa1ed31245d4a36b1ef5a79150324b3', // Add your API key here
|
||||
@@ -20,34 +20,37 @@ const fetchleagueesData = async (req, res, next) => {
|
||||
});
|
||||
|
||||
// Extract relevant data from the API response
|
||||
const leagues = response.data.leagues.map(league => ({
|
||||
const league_data = response.data.standings[0].table.map(team => ({
|
||||
competition: {
|
||||
league_id: league.competition.id,
|
||||
league_name: league.competition.name,
|
||||
league_emblem: league.competition.emblem
|
||||
league_id: response.data.competition.id,
|
||||
league_name: response.data.competition.name,
|
||||
league_emblem: response.data.competition.emblem
|
||||
},
|
||||
standings: {
|
||||
table: {
|
||||
league_position: league.standings.table.position,
|
||||
team_id: league.standings.table.team.id,
|
||||
team_name: league.standings.table.team.name,
|
||||
team_crest: league.standings.table.team.crest
|
||||
league_position: team.position,
|
||||
team_id: team.team.id,
|
||||
team_name: team.team.name,
|
||||
team_crest: team.team.crest
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
// Concatenate leaguees data to allleaguees array
|
||||
allLeagues = allLeagues.concat(leagues);
|
||||
// Concatenate leagues data to allLeagues array
|
||||
allLeagues = allLeagues.concat(league_data);
|
||||
}
|
||||
|
||||
// Attach all leaguees data to res.locals
|
||||
// Attach all leagues data to res.locals
|
||||
res.locals.leagues = allLeagues;
|
||||
next();
|
||||
} catch (error) {
|
||||
console.error('Error fetching leaguees data:', error);
|
||||
}
|
||||
|
||||
catch (error)
|
||||
{
|
||||
console.error('Error fetching leagues data:', error);
|
||||
res.locals.leagues = []; // Set an empty array if there's an error
|
||||
next(); // Call next middleware or route handler
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = fetchleagueesData;
|
||||
module.exports = fetchLeaguesData;
|
||||
|
||||
Reference in New Issue
Block a user