Please check the below code
// This is a CRON job
// This function will walk through the FCM records everyday and remove records older than 2 months
const mysql = require('mysql2/promise');
const errorCodes = require('source/error-codes');
const PropertiesReader = require('properties-reader');
const prop = PropertiesReader('properties.properties');
const pool = mysql.createPool({
connectionLimit : 10,
host: prop.get('server.host'),
user: prop.get("server.username"),
password: prop.get("server.password"),
port: prop.get("server.port"),
database: prop.get("server.dbname")
});
exports.fcmManager = async (event, context) => {
const connection = await pool.getConnection();
context.callbackWaitsForEmptyEventLoop = false;
connection.config.namedPlaceholders = true;
try {
await connection.beginTransaction();
//Get the current datetime
const currentDateTime = new Date();
currentDateTime.setMonth(currentDateTime.getMonth() - 2);
console.log(currentDateTime.toLocaleDateString());
//Delete the records
const deleteFcmSql = "DELETE FROM fcm WHERE date_created < :date ";
const deleteFcmResult = await connection.query(deleteFcmSql, {date:currentDateTime});
} catch (error) {
console.log(error);
await connection.rollback();
return errorCodes.save_failed;
}
finally{
connection.release();
}
};
In my database I have a records which is created on 2022-07-20 05:01:41. So technically, this record needs to get deleted because this code console.log(currentDateTime.toLocaleDateString()); prints the date 7/21/2022.
However, nothing get deleted and no change is taking place in the database. What is wrong here?