尝试部署Firebase功能时

尝试部署Firebase功能时

本文介绍了“意外的令牌条";尝试部署Firebase功能时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Firebase Functions将Stripe合并到iOS应用中.我正在使用带有后端后端的Swift中的Stripe文档接受付款".我首先做了 npm install --save stripe .最后没有错误.然后,我做了 npm install .到目前为止,我的 index.js 看起来像这样:

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

运行 firebase deploy 时,我得到: 11:29错误解析错误:意外的令牌条.我文件中的第11行char 29是 stripe.paymentIntents ...

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

这是我第一次使用Firebase Functions或Stripe,所以我很茫然.感谢您的帮助.

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

这是我的 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
}

推荐答案

此错误是因为在云环境中未安装条带库.

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

npm install 确实会安装依赖项,但要在本地环境中将它们安装在Cloud Functions环境中,您需要从Cloud Function编辑 package.json 文件

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.

添加依赖项该功能需要的.

这是通过将依赖项部分添加到 package.json 文件

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

它会类似:

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

编辑

使用此代码,它可以在Cloud函数上工作:

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

显然,问题在于等待,因为HTTP云功能以同步方式工作

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

这篇关于“意外的令牌条";尝试部署Firebase功能时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 06:12