2 回答
TA贡献1794条经验 获得超8个赞
你错过了return链接承诺的声明Request.findOne()。
let docs = {
total: 0,
total_downloaded: 0,
plan_type: null,
};
Document.findAll({
where: {
report_request_id: req.params.requestId
},
attributes: ["id", "folder_name", "total_file"],
})
.then(documents => {
documents.forEach(document => {
docs.total += 1;
if (document.get("status") == 1) {
docs.total_downloaded += 1;
}
});
})
.then(function() {
return Request.findOne({
where: {
id: req.params.requestId
}
})
.then(request => {
//Suppose I got error here
docs.plan_type = request.plan_type;
})
.catch(err => {
// Block A
throw err;
});
})
.then(function() {
res.status(200).send(docs);
})
.catch(err => {
// Block B
res.status(400).send(err);
});
TA贡献1817条经验 获得超14个赞
您可以使用 Promises 或 async/await 在一个 catch 块中捕获错误。您可以专门在每个使用 promise 的 await 函数中抛出错误。就像是。
const a = new Promise((resolve, reject) => {
try {
// do something
resolve(output);
} catch (error) {
reject(error);
}
});
const b = //... similar to a();
const c = //... similar to a();
const main = async () => {
try {
const f = await a();
const i = await b(f); // if data from a is to be passed to b
const y = await c(i); // if data from a is to be passed to b
return y;
} catch(error){
// catch all errors
throw new Error( error );
}
}
main().then(( data )=>{
// on success do something with data
}).catch((error)=>{
//catch all errors and do something
});
添加回答
举报