Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda