'Delete record on database with cron scheduler does not update if I wont save server

I am using NodeJS cron-schedule to delete a record from the database.

I created a separate module that is executed in app.js file. The Module take all the users (silencedUsers) from the database and map trough them to get the time when the specific user should be deleted from the silenced table.

However, I face some challenges. If I input a record on the database and do not save the server with control + s, the function is not execute.

const {getAllSilencedUsers, deleteSilencedUser} = require('../dataAccessLayer/usersLayers.js');
let cron = require('node-schedule');

module.exports = async () => {
    try 
    {
        const silencedList = await getAllSilencedUsers();

        if (silencedList) {
            silencedList.map(user => {
              
                let time = {
                    year:       user.end_date.getFullYear(),
                    month:      user.end_date.getUTCMonth() + 1,
                    day:        user.end_date.getUTCDate(),
                    hour:       user.end_date.getHours(),
                    minutes:    user.end_date.getMinutes()
                };

                let deadline = `${time.year}-${time.month}-${time.day} ${time.hour}:${time.minutes}:00`;
                cron.scheduleJob(deadline, function(){
                    deleteSilencedUser(user.userID)
                    console.log('Deleted');
                  });
            })
        }
        else return;
    } 

    // Catch block
    catch (error) 
    {
        if (error) console.log(error);
    }
} 

Above is the function that create me problems. I exported it and import it into app.js

/* Node Cronjobs */
require('./services/automaticUnsilence.js')();

The main problem is: When I am input a record in the database and don't save the server, the function does not execute. Please, how can I fix this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source