嵌套警告-避免嵌套问题

时间:2018-09-08 19:02:56

标签: javascript firebase google-cloud-functions

我已经尝试解决嵌套问题,但是我没有用过,甚至没有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);
})

});

标记为 //避免嵌套承诺的行是问题所在... 任何帮助将不胜感激。

编辑-答案结果

screenshot of Terminal

第111:20行显示:     return paypal.payout.create(payReq,sync_mode,(错误,付款)=> {

第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-设计说明中的回复

我拥有的代码:

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

1 个答案:

答案 0 :(得分:1)

也未经测试,但似乎基于您的随从的目标是取消所有内容的嵌套。我觉得这有点麻烦,但确实可行。

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

  // Returns paymentRequest
  const buildPaymentRequest = (value) => {
    return new Promise((resolve) => {
      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"
        }]
      });
      resolve(paymentRequest);
    });
  }

  // Returns payout
  const createPayout = (paymentRequest) => {
    return new Promise((resolve, reject) => {
      paypal
        .payout
        .create(payReq, sync_mode, (error, payout) => {
          if (error) {
            console.warn(error.response);
            reject(error);
          } else {
            console.info("payout created");
            resolve(payout);
          }
        });
    });
  };

  getPayoutsPending(uid)
    .then(getPayoutsAmount)
    .then(buildPaymentRequest)
    .then(createPayout)
    .then(updatePaymentsPending(uid, sender_batch_id))
    .then(() => {
      response.status('200').end();
      return;
    })
    .catch((err) => {
      console.log(err);
      response.status('500').end();
      return console.error(error);
    })
});

或者,将// eslint-disable放在文件顶部可以解决您的问题;)