在下面的代碼片段中,我想驗證第一個異步方法中的字段。使用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