“Unexpected token stripe” when trying to deploy Firebase Functions

。_饼干妹妹 提交于 2020-11-30 00:28:11

问题


I'm trying to incorporate Stripe into an iOS app using Firebase Functions. I'm following the Stripe documentation for "Accepting a payment" in Swift with Node backend. I first did npm install --save stripe. That finished with no errors. Then I did npm install. My index.js looks like this so far:

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });

const functions = require('firebase-functions');
const stripe = require('stripe')('sk_test_...');

const paymentIntent = await stripe.paymentIntents.create({
  amount: 1099,
  currency: 'usd',
});
const clientSecret = paymentIntent.client_secret

When running firebase deploy I get: 11:29 error Parsing error: Unexpected token stripe. Line 11 char 29 in my file is the stripe.paymentIntents...

This is my first time using Firebase Functions or Stripe, so I'm at a loss here. I appreciate any help.

EDIT:

Here's the contents of my package.json file.

{
  "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": "8"
  },
  "dependencies": {
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.1",
    "stripe": "^8.55.0"
  },
  "devDependencies": {
    "eslint": "^5.12.0",
    "eslint-plugin-promise": "^4.0.1",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

回答1:


This error is because on the cloud environment the stripe library is not installed before you require it.

npm install does install the dependencies but in your local environment, to install them on the Cloud Functions envirornment you need to edit the package.json file from the Cloud Function.

This to add the dependencies that it will be required by the function.

This is done by adding the dependency section to the package.json file

It will lok something like:

{
  "name": "sample-name",
  "version": "0.0.1",
  "dependencies": {
    "escape-html": "^1.0.3",
    "stripe": "^8.24.0"
  }
}

EDIT

With this code it works on Cloud functions:

const stripe = require('stripe')('<My Secret Key>');

exports.helloWorld = (req, res) => {
  let paymentIntent = null;

  paymentIntent = stripe.paymentIntents.create({
  amount: 2000,
  currency: 'usd',
  description: 'My first payment',
});
  let message = req.query.message || req.body.message || 'Hello World!';
  res.status(200).send(message);
};

Apparently the issue was the await because HTTP Cloud Functions work on a Synchronous way



来源:https://stackoverflow.com/questions/61999684/unexpected-token-stripe-when-trying-to-deploy-firebase-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!