2 回答

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊
你錯(cuò)過了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貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊
您可以使用 Promises 或 async/await 在一個(gè) catch 塊中捕獲錯(cuò)誤。您可以專門在每個(gè)使用 promise 的 await 函數(shù)中拋出錯(cuò)誤。就像是。
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
});
添加回答
舉報(bào)