【问题标题】:Firebase Cloud Functions warning: "Arrow function expected no return value consistent-return"Firebase Cloud Functions 警告:“箭头函数预期没有返回值一致返回”
【发布时间】:2018-12-04 11:25:32
【问题描述】:

由于某种原因,当我部署从未见过的云功能时,我的终端上出现了输出。

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint /Users/xxxxx/Desktop/cloud_functions/cloud_functions_live/functions
> eslint .


/Users/xxxxx/Desktop/cloud_functions/cloud_functions_live/functions/index.js
  38:5  warning  Arrow function expected no return value  consistent-return

✖ 1 problem (0 errors, 1 warning)

index.js 中的第 38 行也是返回 return docRef.doc("/payments/${payment}").get()

我在这里输入更多信息只是因为 stackoverflow 说我有太多代码并且不允许我添加更新。我希望这不会违反政策,因为其他人要求提供更多代码,并且另一个人已经对这个问题表示赞同,表明我已经用有限的细节将我的问题表达得足够清楚,以供其他人识别。

exports.stripeCharge = functions.firestore.document('/payments/{payment}').onCreate((snap, context) => {

    console.log("payment data", snap);

    const payment = snap.data();

    console.log("payment data2", payment.token);
        //console.log("payment data2", payment.token.idempotency_key);


    if (!payment || payment.charge) return;


    var docRef = admin.firestore()
    const idempotency_key = payment.idempotency_key;  // prevent duplicate charges

    //.document(`/payments/${payment}`);
    //var docRef = 

    return docRef.doc(`/payments/${payment}`).get()
            .then(snapshot => {
            console.log("augu", snapshot);
            return snapshot;
            })
            .then(customer => {
                 const amount = payment.amount;
                 const source = payment.token;
                 const currency = payment.currency;
                 const charge = {amount, currency, source};

                 console.log("brett", charge);
                 return stripe.charges.create(charge, { idempotency_key });

            })
            .then(charge => {
                console.log("set charge back");
                return docRef.doc(`/charges/${idempotency_key}`).set(charge);
            })

})

【问题讨论】:

  • 第 38 行是哪一行?你能展示整个函数吗?
  • 顶一个我的错,我会做一个快速编辑
  • 你能展示整个函数吗?
  • 当然,Stackoverflow 只是抱怨输入的代码太多
  • 通常希望您提供重现问题的最少代码。如果您可以将您的功能提炼到最低限度以找出问题所在,那就更容易了。

标签: javascript node.js firebase google-cloud-functions eslint


【解决方案1】:

您的代码中的问题出在这一行:

if (!payment || payment.charge) return;

由于您稍后返回一个承诺,因此您需要在这里返回一个值。例如假的。

eslint 规则要求,如果你从一个函数返回一些东西,它总是返回一个值。如果这不是您想要的,您可以修改规则以允许未定义并返回未定义。

【讨论】:

    猜你喜欢
    • 2021-04-04
    • 2020-01-09
    • 1970-01-01
    • 2022-01-23
    • 2020-05-06
    • 1970-01-01
    • 2017-12-14
    • 2020-10-14
    • 1970-01-01
    相关资源
    最近更新 更多