11
votes

I am using firebase functions for stripe payment integration. This particular function used for register customer with stripe.

Node version 10.15.3 ,

npm version 6.9.0 ,

"ecmaVersion": 6 in .eslintrc.json

const functions = require('firebase-functions');

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

const stripe = require('stripe')(functions.config().stripe.testkey)

exports.createStripeCustomer = functions.auth.user()

          .onCreate(async (user) => {
             const customer = await
             stripe.customers.create({email: user.email});
             await admin.firestore()
               .collection('stripe_customers')
               .doc(user.uid)
               .set({customer_id: customer.id});

           });

The code is same as the firebase platform provide on github example https://github.com/firebase/functions-samples/blob/master/stripe/functions/index.js

Parsing error: Unexpected token =>

and if I change the "ecmaVersion": 6 to "ecmaVersion": 8 in .eslintrc.json

then error is .onCreate(async (user) => {

                            ^

SyntaxError: Unexpected token (

I want to deploy function properly so that user can register on stripe and date store in firebase storage

3
I think there is no need of async just remove async from ur onCreate method exports.createStripeCustomer = functions.auth.user().onCreate((user) => { // ... }); - Alok Mishra
now it give Parsing error: Unexpected token admin - Rav
You get this error when you run the code or it's an eslint error? - maazadeeb
I think code is run when function deploy properly and new user register to firebase authentication . At the time of user creation this code is run. but the error gives these lines in terminal : npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! functions@ lint: eslint . npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the functions@ lint script. - Rav
@ray so it's clearly not liking async/await syntax. But: is this node failing? Because I'm pretty sure node itself can run this just fine. Are you sure this isn't just your linter/transpiler being some older version that doesn't know async/await are real keywords? - Mike 'Pomax' Kamermans

3 Answers

5
votes

Make sure you are running in your local machine node >= 8. For deployment, you should have in your package.json.

{
    //...
    "engines": {
        "node": "8"
    },
    //...  
}

For eslint, to enable parsing for async functions, you should include this in your config:

{
    "parserOptions": {
        "ecmaVersion": 2017
    }
}
3
votes

Looks like you're talking about an eslint error. I've been able to reproduce it in the eslint demo page using ecmaVersion 2015.

I just changed it to ecmaVersion 2017 (the version from when async/await was supported) and the error has gone away (link).

Also, verified the eslint config in the project you're talking about. It's ecmaVersion 2017: link

0
votes

I am new to React-native + firebase function. But with these lines of code, lint issue went out and I can now deploy firebase functions.

Old

{
  "$schema": "./node_modules/@react-native-firebase/app/firebase-schema.json",
  "react-native": {
    "crashlytics_auto_collection_enabled": true,
    "crashlytics_debug_enabled": true,
    "messaging_android_notification_channel_id": "high-priority",
    "messaging_ios_auto_register_for_remote_messages": true
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint"
    ],
    "source": "functions"
  }
}

New

{
  "$schema":"./node_modules/@react-native-firebase/app/firebase-schema.json",
  "react-native": {
      "crashlytics_auto_collection_enabled": true,
      "crashlytics_debug_enabled": true,
      "messaging_android_notification_channel_id": "high-priority",
      "messaging_ios_auto_register_for_remote_messages":true
    }
}