为什么我在创建 firebase 云函数时收到解析错误:意外的令牌 =>?

Jae*_*lik 5 javascript google-cloud-functions

我对 firebase 云功能相当陌生,我正在尝试创建一个云功能,该功能将向 firebase 上新创建的用户发送电子邮件(我将首先使用日志来测试它),但我一直有一个错误解析错误:意外的标记 =>

这是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}` });
})
Run Code Online (Sandbox Code Playgroud)

这是 .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',
  },
};
Run Code Online (Sandbox Code Playgroud)

这是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
}
Run Code Online (Sandbox Code Playgroud)

任何帮助是极大的赞赏。谢谢!

Ral*_*mos 18

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

"scripts": {
    "lint": "eslint",
    ...
},
Run Code Online (Sandbox Code Playgroud)

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

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

parserOptions: {
    parser: 'babel-eslint',
    ecmaVersion: 8,
},
Run Code Online (Sandbox Code Playgroud)

这可能是您的 eslint 理解async/await符号所必需的。

  • 删除“.” 在 package.json 中的“lint”行:“eslint”。成功了 (2认同)