I have a form that has a field that can upload multiple images in a single <input> tag. When I access the filesystem using Node, it seems to queue the callback for reading/writing files asynchronously. Because I have multiple files, I have these calls in a for loop, so the value of i is always array.length by the time the callbacks are hit, causing the object to be undefined.
for (var i = 0; i < req.files.photos.length; i++) {
req.fs.readFile(req.files.photos[i].path, function(err, data) {
if(err) throw err;
// i = req.files.photos.length here
// Test is undefined when the breakpoint on this line is hit for the first time
var test = req.files.photos[i];
// Both print "undefined"
console.log(test.name);
console.log(test.originalFileName);
var newPath = __dirname + "/../public/uploads/" + req.files.photos[i].name;
req.fs.writeFile(newPath, data, function (err) {
if (err) throw err;
console.log("it worked");
});
});
}