Reputation: 636
I am using Multer to get files from requests for my Express API, and I am wondering what the purpose of the error value in the filename callback is. Here is my code:
const multerFile = multer({
storage: multer.diskStorage({
destination: "uploads/",
filename: (req, file, callback) => {
callback(ERROR HERE WHAT IS THIS FOR?, "fileNameHere`);
},
}),
});
Upvotes: 1
Views: 89
Reputation: 370689
In Node, the way possibly-asynchronous callbacks are typically structured is that the first argument is an error, OR the second argument is the success value. For example, you'll very often see patterns like this:
callSomeAPI((error, result) => {
if (error) {
// There was an error, do something with it
handleError(error);
} else {
// Success
handleResults(result);
}
});
This filename
callback is doing the same sort of thing. If you implement some custom logic and want to indicate that the process failed, pass the first argument containing the reason to the callback:
callback('Desired filename contains invalid characters');
Otherwise, leave the first argument nullish:
callback(null, 'fileNameHere');
Upvotes: 1