gpt4 book ai didi

javascript - 嵌套警告——避免嵌套问题

转载 作者:行者123 更新时间:2023-11-30 11:14:22 28 4
gpt4 key购买 nike

我试图解决嵌套问题,但我使用的任何东西都不起作用,甚至 Google Cloud Functions - warning Avoid nesting promises promise/no-nesting .

我该如何重构这个方法?下面是代码。

exports.payout = functions.https.onRequest((request, response) => {
var uid = "nYIAHSYimJMHbMkXqDt9PQ0U3Nf2";

getPayoutsPending(uid).then((array) => {
getPayoutsAmount(array).then((value) => { **// avoid nesting promises**
var valueTrunc = parseFloat(Math.round(value * 100) / 100).toFixed(2);

const sender_batch_id = Math.random().toString(36).substring(9);
const sync_mode = 'false';
const payReq = JSON.stringify({
sender_batch_header: {
sender_batch_id: sender_batch_id,
email_subject: "You have a payment"
},
items: [
{
recipient_type: "EMAIL",
amount: {
value: valueTrunc,
currency: "CAD"
},
receiver: "me@gmail.com",
note: "Thank you.",
sender_item_id: "Payment"
}
]
});

paypal.payout.create(payReq, sync_mode, (error, payout) => {
if (error) {
console.warn(error.response);
response.status('500').end();
throw error;
} else {
console.info("payout created");
console.info(payout);

**// avoid nesting problems**
updatePaymentsPending(uid, sender_batch_id).then(() => {
response.status('200').end();
return;
}).catch((error) => {
return console.error(error);
})
}
});
return null;
}).catch((error) => {
return console.error(error);
})
return null;
}).catch((error) => {
return console.error(error);
})

});

标记为//避免嵌套 promise 的行是问题所在。

编辑 - 答案的结果

screenshot of Terminal

第 111:20 行内容如下: 返回 paypal.payout.create(payReq, sync_mode, (error, payout) => {

第 120:21 行内容如下: }).then(() => {

编辑 #2

将代码更改为@imjared 提供的代码后,出现以下错误:

ReferenceError: sender_batch_id is not defined
at exports.payout.functions.https.onRequest (/user_code/index.js:136:40)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)
at /var/tmp/worker/worker.js:689:7
at /var/tmp/worker/worker.js:673:9
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)

然后:

Function execution took 1327 ms, finished with status: 'crash'

然后:

ReferenceError: paymentRequest is not defined
at Promise (/user_code/index.js:111:17)
at buildPaymentRequest (/user_code/index.js:90:14)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

编辑 #3 - 来自 destenson 帖子的回复

我的代码:

exports.payout = functions.https.onRequest((request, response) => {
return getPayoutsPending(request.body.uid)
.then(array => getPayoutsAmount(array))
.then(value => {
var valueTrunc = parseFloat(Math.round(value * 100) / 100).toFixed(2);
const sender_batch_id = Math.random().toString(36).substring(9);
const sync_mode = 'false';
const payReq = JSON.stringify({
sender_batch_header: {
sender_batch_id: sender_batch_id,
email_subject: "You have a payment"
},
items: [
{
recipient_type: "EMAIL",
amount: {
value: valueTrunc,
currency: "CAD"
},
receiver: request.body.email,
note: "Thank you.",
sender_item_id: "Payment"
}
]
});

return paypal.payout.create(payReq, sync_mode, (error, payout) => {
if (error) {
console.warn(error.response);
response.status('500').end();
throw error;
}
console.info("payout created");
console.info(payout);
return updatePaymentsPending(request.body.uid, sender_batch_id)
}).then(() => {
response.status('200').end();
return null;
});
})
.catch(error => {
console.error(error);
});
});

当应用程序执行时,函数日志显示:

TypeError: Cannot read property 'then' of undefined
at getPayoutsPending.then.then.value (/user_code/index.js:120:15)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

然后:

    { batch_header: 
{ payout_batch_id: '*************',
batch_status: 'PENDING',
sender_batch_header:
{ sender_batch_id: '************',
email_subject: 'You have a payment' } },
links:
[ { href: 'https://api.sandbox.paypal.com/v1/payments/payouts/*******',
rel: 'self',
method: 'GET',
encType: 'application/json' } ],
httpStatusCode: 201 }

然后:

uncaught exception

然后:

ReferenceError: uid is not defined
at paypal.payout.create (/user_code/index.js:119:46)
at IncomingMessage.<anonymous> (/user_code/node_modules/paypal-rest-sdk/lib/client.js:140:13)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)

最后:

Function execution took 1517 ms, finished with status: 'crash'

编辑 #4 - 最终结果

执行应用程序后,函数的以下日志是:

screenshot of function logs

最佳答案

我会通过链接 promise 来解决这个问题,而不是嵌套它们。当您从 then() 回调中返回一个值时,它就变成了一个可以再次使用的新 promise 。

我没有测试过你代码的这个修改版本,但我希望你能理解它的要点:

exports.payout = functions.https.onRequest((request, response) => {
var uid = "nYIAHSYimJMHbMkXqDt9PQ0U3Nf2";

return getPayoutsPending(uid)
.then(array => getPayoutsAmount(array))
.then(value => {
var valueTrunc = parseFloat(Math.round(value * 100) / 100).toFixed(2);
const sender_batch_id = Math.random().toString(36).substring(9);
const sync_mode = 'false';
const payReq = JSON.stringify({
sender_batch_header: {
sender_batch_id: sender_batch_id,
email_subject: "You have a payment"
},
items: [
{
recipient_type: "EMAIL",
amount: {
value: valueTrunc,
currency: "CAD"
},
receiver: "me@gmail.com",
note: "Thank you.",
sender_item_id: "Payment"
}
]
});

return paypal.payout.create(payReq, sync_mode, (error, payout) => {
if (error) {
console.warn(error.response);
response.status('500').end();
throw error;
}
console.info("payout created");
console.info(payout);
return updatePaymentsPending(uid, sender_batch_id)
});
}).then(() => {
response.status('200').end();
return null;
}).catch(error => {
console.error(error);
});
});

希望对您有所帮助。

编辑:成功案例缺少返回空值。我猜你的 linter 对此很挑剔。

编辑:取消嵌套最后一个 then()

关于javascript - 嵌套警告——避免嵌套问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52238496/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com