Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!