Node.js Multer Filename回调中的错误参数是什么?
我使用Multer从ExpressAPI请求中获取文件,我想知道文件名回调中错误值的用途是什么。这是我的密码:Node.js Multer Filename回调中的错误参数是什么?,node.js,file,express,error-handling,multer,Node.js,File,Express,Error Handling,Multer,我使用Multer从ExpressAPI请求中获取文件,我想知道文件名回调中错误值的用途是什么。这是我的密码: const multerFile = multer({ storage: multer.diskStorage({ destination: "uploads/", filename: (req, file, callback) => { callback(ERROR HERE WHAT IS THIS FOR?, "
const multerFile = multer({
storage: multer.diskStorage({
destination: "uploads/",
filename: (req, file, callback) => {
callback(ERROR HERE WHAT IS THIS FOR?, "fileNameHere`);
},
}),
});
在Node中,异步回调通常的构造方式是第一个参数是错误,或者第二个参数是成功值。例如,您经常会看到这样的模式:
callSomeAPI((error, result) => {
if (error) {
// There was an error, do something with it
handleError(error);
} else {
// Success
handleResults(result);
}
});
这个filename
回调也在做同样的事情。如果实现某些自定义逻辑并希望指示进程失败,请将包含原因的第一个参数传递给回调:
callback('Desired filename contains invalid characters');
否则,将第一个参数保留为null:
callback(null, 'fileNameHere');