I need to read a folder to get the names and from them get the bigger date. folder:
code to read folder, get filenames and get the bigger date(except today):
async function getLastDate() {
try {
let filesNames;
let today = getDate();
let returnValue;
fs.readdir(path.join(rutaBase, 'sessionsOffline'), ((err, files) => {
if (err) {
console.log('error leyendo directorio');
return { ok: false, msg: 'Error leyendo directorio en getLastDate', data: null };
}
//Obtener las fechas de los archivos(despues del ultimo '_')
filesNames = files.map(file => {
if (file.includes('_')) {
return file.substr(file.lastIndexOf('_') + 1);
}
});
//Remover archivos sin fecha
let fileNamesFiltrados = filesNames.filter((fileName) => {
return fileName != null;
});
//Remover el archivo de hoy del arreglo
filesNames = fileNamesFiltrados;
let arrDatesWithOutToday = [];
filesNames.forEach(fileName => {
if (fileName != today) {
arrDatesWithOutToday.push(fileName);
}
});
//Convertir arreglo a array de tipo Date
let arrDatesWithOutToday2 = arrDatesWithOutToday.map(val => {
let [year, month, day] = val.split('-');
return new Date(year, month - 1, day);
});
//Obtener la fecha mayor
let maxDate = Math.max.apply(null, arrDatesWithOutToday2);
//Formater la fecha a YYYY-MM-DD
maxDate = new Date(maxDate).toISOString().split('T')[0];
returnValue = maxDate;
return { ok: true, msg: null, data: maxDate };
}));
} catch (error) {
console.log('probando error: ', error);
return { ok: false, msg: 'Error obtenieno fecha de la ultima sesion', data: null };
}
}
I call getLastDate from another function(line 1936):
but in 1937(console.log('date getBalance: ', date)) Im getting 'date getBalance: undefined' then debugging my code I discovered that callback in fs.readdir is a async function someone can help me? I need get the maxDate in getBalance function(line 1937 of photo). thank you in advance