I've got a dropzone.js widget embedded in a webpage served by a node/express app. That all works famously, so I'm adding in the conditionals that provide upload constraints. There are three such constraints; mime type (that one works fine), uploaded file size (also works fine) and number of files (which does not work fine).
Here's a snippet from the code:
var fileSize = req.file.size;
if(fileSize > 200000) {
return res.status(422).json({
error: "File too large."
});
}
var numfiles = fs_filecount(appwd+"data/free/req.session.freeFolderID");
if(numfiles>20) {
return res.status(422).json({
error: "Maximum file count reached."
});
}
I present these two assignments and conditionals like this because this is how they appear in the .js file. The assignment and conditional for the file size works great, but that for the file count does not. The source for the fs_filecount function follows:
fs_filecount = function(path) {
fs.readdir(path,function(err, files){
if(err){
return console.error(err);
} else {
return files.length;
}
});
};
Note that logging the value returned by this file count function directly demonstrates that it is working fine.
The failing is that after the assignment of numfiles, it is undefined, so the file count test always fails, and the constraint of no more than 20 files is never enforced.
EDIT: Simplified subroutine 'fs_filecount' follows:
function fs_filecount(path) {
var count;
fs.readdir(path,function(err,files) {
if(err) throw err;
else {
count = files.length;
}
});
return count;
};