Hello, I have the following code:
var fs = require('fs');
function search(text) {
var response = [];
fs.readdir('documents',(err, files) => {
files.forEach(file => {
if(file.toLowerCase().indexOf(text.toLowerCase()) != -1){
console.log("Encontrado " + file);
response.push(file);
}
})
});
return response;
}
exports.search = search;
What it does is search in a folder all the files that contain in its name ( file
) a certain substring ( text
), the idea is that all the matching files must store their name in the% array response
the problem is that when returning the array it appears empty (print []
)
As you can see, I have a console.log to see if there is a match at any moment and to confirm that this happens, now the problem is that it seems that the line of the push will never be executed ...