繁体   English   中英

为什么在创建 firebase 云 function 时出现解析错误:Unexpected token =>?

[英]Why do I get a Parsing error: Unexpected token => when creating firebase cloud function?

I'm fairly new to firebase cloud functions and I'm trying to create a cloud function that will send an send an email to a newly created user on firebase (i will be using a log to test it first) but I keep having an error 解析错误:意外的令牌 =>

这是 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
}

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

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM