代码之家  ›  专栏  ›  技术社区  ›  Blue

Firebase云函数警告:“箭头函数预期无返回值一致返回”

  •  3
  • Blue  · 技术社区  · 7 年前

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

    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()

    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);
                })
    
    })
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   m0c    7 年前

    代码中的问题在这一行:

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

    eslint规则要求,如果从函数返回某个内容,它将始终返回一个值。如果这不是您想要的,您可以修改规则以允许undefined并返回undefined。