【发布时间】:2019-08-07 19:57:17
【问题描述】:
如何有条件地跳过承诺而不做任何事情。我创建了一个嵌套的 promise,通过它我有 7 个 .then's。但是有条件地,我需要跳过几个 .then 并且在那个块中什么都不做,如何实现这个?
我的完整代码:
const admin = require('firebase-admin');
const rp = require('request-promise');
module.exports = function(req, res) {
const phone = String(req.body.phone).replace(/[^\d]/g, '');
const amount = parseInt(req.body.amount);
const couponCodeName = (req.body.couponCodeName);
const couponUsage = parseInt(req.body.couponUsage);
const usersCouponUsage = parseInt(req.body.usersCouponUsage);
const finalAddress = (req.body.finalAddress);
const planName = (req.body.planName);
const saveThisAddress = (req.body.saveThisAddress);
const orderNumber = (req.body.orderNumber);
const pay_id = (req.body.pay_id);
const options = {
method: 'POST',
uri:`https://..........`,
body: {
amount
},
json: true
};
return admin.auth().getUser(phone)
.then(userRecord => {
return rp(options)
})
.then((orderResponse) => {
return admin.database().ref('trs/'+ phone)
.push({ pay_id: orderResponse.id })
})
.then(() => {
return admin.database().ref('ors/'+ phone)
.push({ pay_id })
})
.then(() => {
return saveThisAddress === true ?
admin.database().ref('address/'+phone)
.push({address: finalAddress}) : null
})
.then(() => {
return admin.database().ref('deliveryStatus/'+phone+'/'+orderNumber)
.set({ plan: planName === "" ? "Single Day Plan" : planName, delivered: false}, () => {
res.status(200).send({ success:true })
})
})
.then(() => {
return couponCodeName === "" ? null :
admin.database().ref(`couponCodes/${couponCodeName}`)
.update({couponUsage: couponUsage + 1 })
})
.then(() => {
return usersCouponUsage === "" ? null :
admin.database().ref(`couponUsage/${phone}`)
.update({ [couponCodeName]: usersCouponUsage + 1 })
})
.catch((err) => {
res.status(422).send({ error: err })
})
.catch((err) => {
res.status(422).send({error: err });
});
}
从上面的代码中,最后两个 .then 有一个条件 return couponCodeName === "" ?空:代码...)}。
我需要实现的是,当 couponCodeName === "" 那么,它应该跳过 .then 块并且什么都不做。但是,我在此返回 null,它会引发未处理的拒绝错误。那么如何实现呢?如何跳过.then,什么都不做(什么都不做很重要,直接跳过它)怎么做?
我得到的错误是:我从这些嵌套的 .then 中得到的错误是“未处理的拒绝”和“错误:发送后无法设置标头。”
来自 Google Cloud 函数的错误
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:369:11)
at ServerResponse.header (/var/tmp/worker/node_modules/express/lib/response.js:767:10)
at ServerResponse.send (/var/tmp/worker/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/var/tmp/worker/node_modules/express/lib/response.js:267:15)
at ServerResponse.send (/var/tmp/worker/node_modules/express/lib/response.js:158:21)
at admin.auth.getUser.then.then.then.then.then.then.then.catch.catch (/user_code/request_payment_details.js:86:28)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
还有
Unhandled rejection
注意:Node Js 版本:6(所以我认为正式,我不能使用 async 和 await)
【问题讨论】:
-
您的代码应该可以工作,但是如果 usersCouponUsage 不是空字符串并且其中一个 admin.database 调用拒绝,那么您最终会收到您描述的错误,因为您在你已经完成
res.status(200).send(...)之后在 catch 中调用res.status(422)
标签: javascript promise es6-promise