创建FireBase云函数时,为什么会出现解析错误:意外令牌=>? [英] Why do I get a Parsing error: Unexpected token => when creating firebase cloud function?

查看:25
本文介绍了创建FireBase云函数时,为什么会出现解析错误:意外令牌=>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对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}` });
})

这是.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表示法所必需的。

这篇关于创建FireBase云函数时,为什么会出现解析错误:意外令牌=>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆