简体   繁体   中英

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 Parsing error: Unexpected token =>

this is the index.js code

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

this is the.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',
  },
};

this is the 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
}

any help is greatly appreciated. thanks!

Two possible solutions to the issue you are facing would be to change in your package.json the scripts section to the following:

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

So, removing the . from there, which is auto-generated but might cause this kind of issues.

Also you could change the ecmaVersion of the parser to version 8, so changing in your .eslintrc.js file to this:

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

This might be needed for your eslint to understand the async/await notation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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