【问题标题】:Firebase Functions + CORSFirebase 函数 + CORS
【发布时间】:2018-03-16 22:14:02
【问题描述】:

我需要使用 cors 的 firebase 功能存在问题。根据文档和我读过的所有帖子,它应该可以工作,但似乎无论我尝试什么,我都会遇到同样的错误:

Failed to load <URL>: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 500.
If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

这是我的 firebase 函数 index.js 文件中的相应代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const cors = require('cors')({origin: true});
const stripe = require('stripe')('<TEST_KEY>');
const gcs = require('@google-cloud/storage')({keyFilename: '<PATH_TO_KEY>'});
const Easypost = require('@easypost/api');
const api = new Easypost('<TEST_KEY>');

admin.initializeApp(functions.config().firebase);

exports.processOrder = functions.https.onRequest((req, res) => {
  cors(req, res, () => {

    var body = JSON.parse(req.body);

    if (
      !body.shipment_id       ||
      !body.items             ||
      !body.card
    ) return res.set('Access-Control-Allow-Origin', '*').send({error: true, message: 'Missing information'});


    getPrices(body.items, (err, prices, totalPrice) => {
      if (err) return res.set('Access-Control-Allow-Origin', '*').send({error: err, message: "Error"})

      // Create a new customer and then a new charge for that customer:
      stripe.customers.create({
        email: 'test@example.com'
      }).then((customer) => {
        return stripe.customers.createSource(customer.id, {
          source: body.card.token.id
        });
      }).then((source) => {
        return stripe.charges.create({
          amount: (totalPrice * 100),
          currency: 'usd',
          customer: source.customer
        });
      }).then((charge) => {
        return res.set('Access-Control-Allow-Origin', '*').send({error: false, message: "Success"});
      }).catch((err) => {
        console.log(err);
        return res.set('Access-Control-Allow-Origin', '*').send({error: err, message: "Error"});
      });

    });
  });
});

任何帮助将不胜感激:)

编辑: 只是想注意:我试过只设置res.set('Access-Control-Allow-Origin', '*') 而不使用 cors 中间件,我试过不设置标题而只使用 cors。两者都不起作用:(

解决方案:正如@sideshowbarker 在评论中所说,我的函数在返回之前在其他地方出现错误。 Access-Control-Allow-Origin 甚至从未设置过。一旦我修复了错误,一切都很好!太棒了!

【问题讨论】:

  • 响应的 HTTP 状态代码为 500 表明发生了一些内部服务器故障——可能在您设置标头的代码运行之前。无论如何,服务器不会将应用程序集标头添加到 500 错误响应中。它可能只会将它们添加到 2xx 成功响应中。所以问题是你得到一个 500 错误而不是 200 成功响应。客户端错误消息中提到 Access-Control-Allow-Origin 并不表示您在服务器上配置错误的 CORS 支持 - 而只是表示发生了其他一些服务器故障。
  • 你在使用 node.js 吗?
  • 嗯,我会看看我的代码,看看是否有任何东西可能导致@sideshowbarker 提到的 500 错误。
  • 是的,它正在使用 node.js @WillemvanderVeen

标签: javascript firebase google-cloud-functions


【解决方案1】:

在节点中你可以使用一个包来解决这个问题。要启用所有 CORS,请安装以下软件包:

npm install cors

然后假设您使用的是express,您可以通过以下代码行启用 CORS:

var cors = require('cors');
app.use(cors());

【讨论】:

    猜你喜欢
    • 2018-08-17
    • 2020-02-12
    • 2018-10-21
    • 2020-07-04
    • 2022-01-20
    • 2020-09-02
    • 2021-03-21
    • 2020-07-07
    • 2020-05-13
    相关资源
    最近更新 更多