【问题标题】:Why do I get a Parsing error: Unexpected token => when creating firebase cloud function?为什么在创建 Firebase 云函数时出现解析错误:Unexpected token =>?
【发布时间】:2021-06-01 21:36:50
【问题描述】:

我对 firebase 云功能相当陌生,我正在尝试创建一个云功能,它将向 firebase 上新创建的用户发送电子邮件(我将首先使用日志对其进行测试),但我一直报错解析错误:Unexpected token =>

这是 index.js 代码

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

const db = admin.firestore();

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//

exports.onUserCreate = functions.firestore.document('users/{usersId}').onCreate(async (snap, context) => {
    const values = snap.data();

    //send email
    await db.collection('logging').add({ description: `Email was sent to user with nickname:${values.username}` });
})

这是.eslintrc.js

module.exports = {
  root: true,
  parserOptions: {
    parser: 'babel-eslint',
    ecmaVersion: 6,
  },
  env: {
    es6: true,
    node: true,
  },
  extends: [
    'eslint:recommended',
    'google',
  ],
  rules: {
    'generator-star-spacing': 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
  },
};

这是 package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "14"
  },
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.14.1"
  },
  "devDependencies": {
    "eslint": "^7.6.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

非常感谢任何帮助。谢谢!

【问题讨论】:

    标签: javascript google-cloud-functions


    【解决方案1】:

    解决您面临的问题的两种可能方法是将您的 package.json 脚本部分更改为以下内容:

    "scripts": {
        "lint": "eslint",
        ...
    },
    

    因此,从那里删除 .,它是自动生成的,但可能会导致此类问题。

    您还可以将解析器的 ecmaVersion 更改为版本 8,因此将您的 .eslintrc.js 文件更改为:

    parserOptions: {
        parser: 'babel-eslint',
        ecmaVersion: 8,
    },
    

    这可能是您的 eslint 理解 async/await 表示法所必需的。

    【讨论】:

    • 删除“.”在 package.json 的“lint”行中:“eslint”。成功了
    猜你喜欢
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 2019-06-16
    相关资源
    最近更新 更多