Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
NodeJS delete specific records when the date reaches 60 days

Good evening. Im Using JSJoda to calculate the difference between two dates. In the database there is a row where by default, the date is inserted automatically. I need to create a normal function (without Req, Res object) where each row will be deleted after 60 days.

const cron = require('node-cron');
const JSJoda = require('js-joda');
const {deleteRecord, getAllMessages} = require('../connection/db.js');

exports.deleteMessage = async () => {
    /**
     *      Get all the messages and delete statement
    */
   const deleteStatement = 'DELETE FROM ?? WHERE ?? = ?';
   const selectStatement = 'SELECT * FROM message';


    try 
    {
        const messageList = await getAllMessages(selectStatement);
        
        /**
         *      Loop over the array of messages and find the date
        */
        for (message of messageList) {
            let dateTarget = message.dateAdded;
            
        /**
         *      Convert the date to ISOString() and leave only the dates
         */
            let messageDates = dateTarget.toISOString().split('T')[0];
            let today = new Date().toISOString().split('T')[0];

            let difference = JSJoda.ChronoUnit.DAYS.between(JSJoda.LocalDate.parse(messageDates), JSJoda.LocalDate.parse(today));
            
            while (difference === 0) {
                // Problem is here I dont know what should I do
                await deleteRecord(deleteStatement, 'message', message.messageID, value);
            }
        }
    } 
    
    catch (error) 
    {
        console.log(error);
    }
};

In this function I get all the records, and parsed it to the required format by the library. The variable bellow apparently shows the right days, But now I realised that I am stuck.

let difference = JSJoda.ChronoUnit.DAYS.between(JSJoda.LocalDate.parse(messageDates), JSJoda.LocalDate.parse(today));

I dont know how should I proceed with this function.... Where to place it, and the most important, how to select the right messageID that must be deleted.

This is the actual columns from MySQL enter image description here

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda