1

The problem that I am running into is that I am trying to create an express route that I can run a seed script or cron job against which when hit will then run a GET request, with Axios against an external API endpoint on a scheduled basis, get the appropriate data, and store it in a MongoDB database.

I was able to successfully run this in my backend app.js file, but the problem is that I don't want to run this query every single time the backend app starts or restarts as it will input duplicate data into the database and will not scale, which is why I've separated it out into Models and Routes and am trying to create an endpoint that I can hit with a cron job or seed script.

I have created a Model that will create the appropriate Schema and Model in MongoDB. The model creates fine, which I can see when I restart my backend server.

I have created a Route called /schedule that when hit should run the appropriate code to:

  1. Use the Mongo Schema and Model
  2. Run axios.get against the API endpoint
  3. Use an onSuccess function to query the JSON response data, pull out the appropriate fields, and then send that data up to my MongoDB collection.

Here is my schedule.Route.js file:

const express = require("express"); 
const router = express.Router(); 
const axios = require('axios'); 
const Schedule = require("../models/scheduleModel"); 
const { response } = require('express')

var date_ob = new Date(); 
var day = ("0" + date_ob.getDate()).slice(-2);
var month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
var year = date_ob.getFullYear();
var date = year + "/" + month + "/" + day; 

// Route
router.get("/schedule", (response) => {

axios.get('https://api.sportradar.us/ncaamb/trial/v7/en/games/' + date + '/schedule.json?api_key=' + apiKey)
.then(function (response) {
    onSuccess(response)
  })
  .catch(function (error) {
    console.log(error);
  });
}); 

function onSuccess(response) {
  var gameData = response;
  var gameArray = gameData.data.games
  var arrayLength = Object.keys(gameArray).length 
    console.log(arrayLength)
   for(var i = 0; i < arrayLength; i++) {
      var homeTeam = gameData.data.games[i].home.name;
      var homeAlias = gameData.data.games[i].home.alias;
      var homeId = gameData.data.games[i].home.id;
      var awayTeam = gameData.data.games[i].away.name;
      var awayAlias = gameData.data.games[i].away.alias;
      var awayId = gameData.data.games[i].away.id;
      console.log( homeTeam + " " + homeAlias + " " + homeId + " " + awayTeam + " " + awayAlias + " " + awayId);

      assignScheduleValue(homeTeam, homeAlias, homeId, awayTeam, awayAlias, awayId)
    }
}

// Store values in MongoDB

function assignScheduleValue(homeTeam, homeAlias, homeId, awayTeam, awayAlias, awayId) {

  var upSchedule = new Schedule()
    upSchedule.homeAlias = homeAlias; 
    upSchedule.homeTeam = homeTeam;
    upSchedule.awayTeam = awayTeam; 
    upSchedule.homeId = homeId; 
    upSchedule.awayId = awayId; 
    upSchedule.awayAlias = awayAlias; 
 
    upSchedule.save();
 }

module.exports = router; 

From my app.js file I'm just running:

app.use("/schedule", require("./routes/scheduleRoute"));

Again the goal with this route is so that I can hit it with a CURL or some other command and just run the axios.get request to pull that data into MongoDB daily.

Mike Tower
  • 17
  • 2

0 Answers0