简体   繁体   English

云 Function Firebase request.body 未定义

[英]Cloud Function Firebase request.body is undefined

User side: POST method using fetch function:用户端:使用 fetch function 的 POST 方法:

let response = await fetch(url, {
  "method": "POST",
  "headers": {
    "Content-Type": "application/json"
  },
  "body": JSON.stringify({"stripeToken": token.id})
});

Server side: URL to fetch is a Firebase Cloud Function:服务器端:URL 要获取的是 Firebase 云 Function:

const functions = require('firebase-functions');
const cors = require('cors')({origin: true});
const stripe = require("stripe")("PRIVATE_KEY");

exports.chargeStripe = functions.https.onRequest( async (request, response) => {

    // Getting token
    let token = request.body.stripeToken;

    // Creating charge
    let charge = await stripe.charges.create({
        amount: 99,
        currency: 'eur',
        description: 'Payment',
        source: token
    });

    // Sending response
    charge ? send(response, 200, {message: 'Success'}) : send(response, 500, {message: 'Error!'});

});

The problem is that I am getting console.log(token) → 'undefined' .问题是我得到了console.log(token) → 'undefined'

Update 1更新 1

It seems that Chrome (browser) is blocking the fetch function (CORS policy). Chrome(浏览器)似乎阻止了获取 function(CORS 策略)。 If I test the end-point via Insomnia, everything is working fine.如果我通过 Insomnia 测试端点,一切正常。

So, someone knows what to do in this case?那么,有人知道在这种情况下该怎么办吗?

Update 2更新 2

Thanks to Doug Stevenson , I added CORS middleware inside the function:感谢Doug Stevenson ,我在 function 中添加了 CORS 中间件:

exports.chargeStripe = functions.https.onRequest( (request, response) => {

    // Enable CORS using the `cors` express middleware.
    return cors(request, response, async () => {

        // Reading token
        let token = request.body.stripeToken;

        // Charging user
        let charge = await stripe.charges.create({
            amount: 99,
            currency: 'eur',
            description: 'Payment',
            source: token
        });

        // Sending response 
        response.status(200).send('Ok');
    });

}); 

Now everything works!现在一切正常!

Thanks to Doug Stevenson , I added the cors package :感谢Doug Stevenson ,我添加了cors package

const cors = require('cors')({origin: true});

And then, I used CORS middleware inside the function.然后,我在 function 中使用了 CORS 中间件。 So, the final solution is:所以,最终的解决方案是:

const functions = require('firebase-functions');
const cors = require('cors')({origin: true});
const stripe = require("stripe")("PRIVATE_KEY");

exports.chargeStripe = functions.https.onRequest( (request, response) => {

    // Enable CORS using the `cors` express middleware.
    return cors(request, response, async () => {

        // Reading token
        let token = request.body.stripeToken;

        // Charging user
        let charge = await stripe.charges.create({
            amount: 99,
            currency: 'eur',
            description: 'Payment',
            source: token
        });

        // Sending response 
        response.status(200).send('Ok');
    });

}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM