如何修复节点中的“解析错误:意外令牌 =>”?

Rav*_*Rav 10 javascript node.js stripe-payments firebase google-cloud-functions

我正在使用 firebase 函数进行条带支付集成。此特定功能用于使用条带注册客户。

节点版本 10.15.3 ,

npm 版本 6.9.0 ,

"ecmaVersion": 6 in .eslintrc.json

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

const stripe = require('stripe')(functions.config().stripe.testkey)

exports.createStripeCustomer = functions.auth.user()

          .onCreate(async (user) => {
             const customer = await
             stripe.customers.create({email: user.email});
             await admin.firestore()
               .collection('stripe_customers')
               .doc(user.uid)
               .set({customer_id: customer.id});

           });

Run Code Online (Sandbox Code Playgroud)

代码与 github 示例https://github.com/firebase/functions-samples/blob/master/stripe/functions/index.js上提供的 firebase 平台相同

解析错误:意外的令牌 =>

如果我将 .eslintrc.json 中的 "ecmaVersion": 6 更改为 "ecmaVersion": 8

then error is .onCreate(async (user) => {

                            ^

SyntaxError: Unexpected token (
Run Code Online (Sandbox Code Playgroud)

我想正确部署功能,以便用户可以在 firebase 存储中的条带和日期存储上注册

ajo*_*era 5

确保您在本地机器节点上运行 >= 8。对于部署,您应该在 package.json 中有。

{
    //...
    "engines": {
        "node": "8"
    },
    //...  
}
Run Code Online (Sandbox Code Playgroud)

对于 eslint,要启用异步函数解析,您应该在配置中包含以下内容:

{
    "parserOptions": {
        "ecmaVersion": 2017
    }
}
Run Code Online (Sandbox Code Playgroud)


Maa*_*eeb 4

看起来您正在谈论 eslint 错误。我已经能够使用 ecmaVersion 2015 在 eslint 演示页面中重现它。

我刚刚将其更改为 ecmaVersion 2017(支持时的版本async/await)并且错误已经消失(链接)。

另外,验证了您正在讨论的项目中的 eslint 配置。这是 ecmaVersion 2017:链接