[英]What is the Error Parameter in The Multer Filename Callback For?
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.我正在使用 Multer 从我的 Express API 请求中获取文件,我想知道文件名回调中错误值的目的是什么。 Here is my code:
这是我的代码:
const multerFile = multer({
storage: multer.diskStorage({
destination: "uploads/",
filename: (req, file, callback) => {
callback(ERROR HERE WHAT IS THIS FOR?, "fileNameHere`);
},
}),
});
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.在 Node 中,可能异步回调的典型结构是第一个参数是错误,或者第二个参数是成功值。 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.这个
filename
回调正在做同样的事情。 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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.