【发布时间】:2018-05-23 21:31:09
【问题描述】:
代码是更大更复杂代码的一部分,所以我只是将相关的 sn-ps 放到我的问题中。
我有这个promise.all sn-p:
Promise.all(receivedObjs.arrayCMsIds.map(cmid =>
server.writeAttachedCMinfo(invId,cmid)))
.then(function (results) { // for promise all
return res.json(apiHelp.success(results,"success"));
}).catch(function (error) {
res.json(apiHelp.error(error, error));
});
还有这个冗长复杂的 writeAttachedCMinfo 函数:
server.writeAttachedCMinfo = function (invId,cmid) {
return new Promise(function (resolve, reject) {
console.log("writeAttachedCMinfo");
console.log("invoiceId " + invId);
console.log("cmid "+ cmid);
var invoiceId = JSON.stringify(invId);
var cmId = JSON.stringify(cmid);
var invIdString = invoiceId;
var cmIdString = cmId;
invIdString = invIdString.slice(1, -1);
cmIdString = cmIdString.slice(1, -1);
var projection = 'gwCode certifiedInvoiceAmount buyerReference supplierReference invoiceNo invoiceSerialNo invoiceFiles creditMemos';
ubiqInvoice.findById(invIdString, projection).then(function (dbInvoice) {
var intInvCertifiedAmount = parseInt(dbInvoice.certifiedInvoiceAmount);
creditMemo.findById(cmIdString).then(function (dbCreditMemo) {
var intCreditMemoAmount = parseInt(dbCreditMemo.creditMemoAmount);
if (intInvCertifiedAmount <= intCreditMemoAmount) {
console.log('cm bigger than invoice')
return new Error ('CMisbiggerThanInvoice');
}
if (dbCreditMemo.isAssociated) {
return new Error ('CMisAssociated');
}
if (dbInvoice.gwCode === "100000000000"
|| dbInvoice.gwCode === "110000000000"
|| dbInvoice.gwCode === "111200000000"
|| dbInvoice.gwCode === "111100000000"
|| dbInvoice.gwCode === "111110000000"
) {
var creditMemoEntry = {
id: guid.create().value,
batchId: dbCreditMemo.batchId,
invoiceId: dbInvoice._id,
recordTypeCode: "CM",
buyerReference: dbInvoice.buyerReference,
supplierReference: dbInvoice.supplierReference,
creditMemoNo: dbCreditMemo.creditMemoNo,
creditMemoIssuingDate: dbCreditMemo.creditMemoIssuingDate,
creditMemoEffectiveDate: dbCreditMemo.creditMemoEffectiveDate,
lastModificationDate: dbCreditMemo.lastModificationDate,
currencyCode: dbCreditMemo.currencyCode,
creditMemoAmount: dbCreditMemo.creditMemoAmount,
hashCode: dbCreditMemo.hashCode,
description: dbCreditMemo.description,
uploadDate: dbCreditMemo.uploadDate,
isAssociated: true,
}
dbInvoice.creditMemos.push(creditMemoEntry);
dbInvoice.certifiedInvoiceAmount = dbInvoice.certifiedInvoiceAmount - dbCreditMemo.creditMemoAmount;
dbInvoice.save();
dbCreditMemo.isAssociated = true;
dbCreditMemo.save();
resolve(dbInvoice)
}
else { return new Error ('wrongggwcode'); }
})
});
}), function (error) {
console.log("error: " + error);
}
}
我的目标是在不满足 if 条件之一的情况下强制抛出错误,并且我想以自定义消息的形式将错误传递给客户端,以便我可以在客户端上使用它来表示显示各种错误,例如'CMisbiggerThanInvoice'
if (intInvCertifiedAmount <= intCreditMemoAmount) {
console.log('cm bigger than invoice')
return new Error ('CMisbiggerThanInvoice');
}
我只是想找出一种方法将错误从 writeAttachedCMinfo 函数传递给 promise.all 的 .catch(function (error) 但它不起作用,即使其中一个 promise.all 总是返回成功如果条件不满足。
我试过reject('CMisbiggerThanInvoice')、reject(new Error('CMisbiggerThanInvoice')...都一样。
如何才能真正强制 promise 函数返回错误?
【问题讨论】:
-
调用
reject应该有效。当没有错误时,您是否获得了预期的results?另外请修复代码缩进,以便我们可以轻松解析嵌套。 -
嗨,Bergi,是的,当没有错误时,我得到了预期的结果 - 但当出现错误时,没有任何结果,例如拒绝(新错误('fail'))不起作用,或者是还有其他方法可以拒绝吗?
-
这两个存档怎么样?它们是同步的还是异步的?
-
另外,您是否意识到,因为只有一个
invId,所以只有一个dbInvoice,并且不需要在每次调用server.writeAttachedCMinfo()时从数据库中一遍又一遍地检索它。事实上,它可能不应该被一遍又一遍地检索,否则每个dbInvoice.save()都有覆盖同一整体事务中早期保存的危险。累积所有 creditMemo 并逐渐减少certifiedInvoiceAmount然后执行单个 `dbInvoice.save() 似乎更安全。
标签: javascript node.js promise