I am using Node.js and the fs
module. When a file is uploaded, I want to rename it to its original name and log the updated name (original name). My code for renaming an uploaded file works fine. But I am having trouble printing fileName
.
let fileName = "";
form.on('file', function(field, file) {
fs.rename(file.path, path.join(form.uploadDir, file.name), function(err) {
if (err != null) {
console.log(err);
}
fileName = path.join(form.uploadDir, file.name);
});
});
let name = fileName;
console.log("NAME " + name);
What should I do differently?
EDIT: This is not a duplicate as the question really is specifically about fs.rename
, not how to bypass asynchronous property