【发布时间】:2018-03-11 12:12:29
【问题描述】:
在下面的 sn-p 中,我想验证第一个异步方法中的字段。
如果它们无效,我想立即向用户返回一个错误。
我该怎么做?
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);
});
【问题讨论】:
-
您可以立即从您正在检查字段的 if 块返回带有错误
return callback(err)的回调,该回调将直接执行您发送response code的callback handler function。
标签: javascript node.js async.js formidable