Hey,
I work with API that refreshing every 10 sec, and check if we got new Games.
if we have a new Game I need to create a Game and save it to MongoDB and push the to his league.
I don’t succeed to find to way to check if the game exists or not…
plsss help me
here’s my code :
req.end(function (res) {
if (res.error) throw new Error(res.error);
var games = res.body.response;
for(var j=0;j<games.length;j++){
var currentGame = games[j];
Game.find({game_id: currentGame.fixture.id})
.then(game_check=>{
console.log(currentGame.fixture.id)
if(game_check==""){ // The Game is not Exist mean create a new Game and push it to his league
console.log(currentGame.fixture.id)
var league=currentGame.league.name
var league_id=currentGame.league.id
var game_id=currentGame.fixture.id
var homeTeam=currentGame.teams.home.name
var awayTeam=currentGame.teams.away.name
var dateGame=currentGame.fixture.date
const new_game = new Game({
league,
league_id,
game_id,
homeTeam,
awayTeam,
dateGame
});
new_game
.save()
.catch((err) => {
console.log(err);
})
League.findByIdAndUpdate(
leagueID,
{
$push: { upcoming: new_game },
},
{
new: true,
})
.exec((err, result) => {
if (err) {
return res.status(422).json({ error: err });
} else {
}
});
}
else{
console.log("already exist"+currentGame.fixture.id);
}
})}
});