I think I’m having trouble with variable scope. Ultimately I’m creating a text log containing certain files and their date last modified using this code:
var fs = require('fs');
fs.writeFileSync('./scan_log.txt', "");
for(e in extensions){
createScanLogHeader(extensions[e]);
for (l in lines){
if(lines[l].indexOf(extensions[e]) > -1){
var fileMtime = getFileProperty(lines[l], "mtime");
fs.appendFileSync('./scan_log.txt', fileMtime + " " + lines[l] + "\n");
}
}
}
function getFileProperty(path, prop){
fs.stat(path, function(err, stats){
return stats.prop;
});
}
I get “undefined” prepended before the file path instead of the mtime file property. However if I put a console.log(stats.prop) in the getFileProperty function right above return stat.prop I do get the correct information logged to the console.
Your
statcall is asynchronous, and yourgetFilePropertyfunction has noreturnvalue defined, so it returnsundefined.Either use
statSyncand return its value, or pass a callback togetFileProperty.