簡體   English   中英

嘗試部署 Firebase 功能時出現“意外的令牌條帶”

[英]"Unexpected token stripe" when trying to deploy Firebase Functions

我正在嘗試使用 Firebase 函數將 Stripe 合並到 iOS 應用程序中。 我正在使用 Node 后端在 Swift 中關注“接受付款”的 Stripe 文檔。 我首先做了npm install --save stripe 沒有錯誤就完成了。 然后我做了npm install 到目前為止,我的index.js看起來像這樣:

// // 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

運行firebase deploy時,我得到: 11:29 error Parsing error: Unexpected token stripe 我文件中的第 11 行字符 29 是stripe.paymentIntents ...

這是我第一次使用 Firebase Functions 或 Stripe,所以我在這里不知所措。 我感謝任何幫助。

編輯:

這是我的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": "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
}

此錯誤是因為在雲環境中,在您需要之前未安裝條帶庫。

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.

這是為了添加 function 所需的依賴項。

這是通過將依賴部分添加到package.json文件來完成的

它看起來像:

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

編輯

使用此代碼,它適用於雲功能:

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

顯然問題是等待,因為 HTTP 雲功能以同步方式工作

我看到這個問題很舊,但我遇到了這個確切的問題,找不到答案。

我的后端函數文件夾嵌套在我的整個應用程序中,我讓 firebase 為我生成了一些文件,包括 lint 配置。 所以,我最終在我的項目中得到了兩個 lint 配置文件。 firebase 生成的一個嘗試強制使用雙引號,而我創建的那個嘗試強制使用單引號。 我最后只是刪除了生成的 lint 配置,現在它工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM