在下面的代码片段中,我想验证第一个异步方法中的字段。使用Node.js,Async和Formidable处理错误
如果它们无效,我想立即向用户返回错误。
我该怎么做?
var form = new formidable.IncomingForm();
async1.series([
function (callback) {
form.parse(req);
form.on('field', function (name, val) {
// Get the fields
});
form.on('fileBegin', function (name, file) {
if (file.name !== "") {
file.path = __dirname + '/upload/' + file.name;
}
});
callback();
},
function (callback) {
form.on('file', function (name, file) {
try {
// Do something with the file using the fields retrieved from first async method
}
catch (err) {
logger.info(err);
}
});
callback();
}
], function (err) {
//the upload failed, there is nothing we can do, send a 500
if (err === "uploadFailed") {
return res.send(500);
}
if (err) {
throw err;
}
return res.status(200);
});
您可以返回一个错误回调'返回回调(ERR)'立即从if块,你正在检查的领域,即回调将直接执行你的'回调处理器function'你在哪里发送'响应代码'。 – Zeeshan