I have a query regarding MongoDB and JavaScript (Express.js).
Here is the scenario:
I am working on developing a backend service for movies and TV series. I have already set up basic routes and functions. One of my requirements is to select a movie from the database and display it as "Movie Of The Day".
One of my functions is:
function filterFilmsForImdb(films, imdb){
let filmChances = [];
films.filter(film => {
if(film.imdbScore >= imdb){
filmChances.push(film);
};
});
return filmChances;
};
// Movie Of The Day
function movieOfTheDay(films) {
const date = new Date();
if (date.getHours() >= 0) {
let arr = filterFilmsForImdb(films, 7);
let numberOfFilmChances = arr.length;
let randomFilmPicker = Math.floor(Math.random() * numberOfFilmChances);
return arr[randomFilmPicker]; // returns object (film object)
};
};
// Recommended Films
function recommendedMovies(films){
return filterFilmsForImdb(films, 5);
};
I have written a function that runs after midnight (00:00), but when I refresh the page, it continues to display random data.
Is there a way to modify this function so that it only selects a movie randomly once per day?
Should I provide more details about my routes to clarify my question? Please let me know.