admin 管理员组文章数量: 1086019
save time and skip to answer
What's wrong with my axios 'GET' this.props.profile.id
logs into the console correctly from react-redux-firebase
, but that's another story.
...
const response = await axios.get(
"",
{ 'seatsioid': this.props.profile.id }
);
Or cloud functions header?
const functions = require('firebase-functions');
//const admin = require('firebase-admin');
const { SeatsioClient } = require('seatsio')
const cors = require('cors')
//admin.initializeApp(functions.config().firebase);
// // Create and Deploy Your First Cloud Functions
// //
//
exports.listCharts = functions.https.onRequest(async(req, res) => {
cors(req, res, async () => {
res.set('Access-Control-Allow-Origin', '');
//res.set('Access-Control-Allow-Credentials', 'true');
res.set('Access-Control-Allow-Methods', 'GET');
res.set('Access-Control-Allow-Headers', 'Content-Type', 'X-Requested-Width', 'Accept', 'seatsioid')
res.set('Access-Control-Max-Age', '60');
var allowedOrigins = ['', ''];
var origin = req.headers.origin;
if(allowedOrigins.indexOf(origin) > -1){}
const seatsioid = **req.get('seatsioid')
let clientAdmin = new SeatsioClient('<THIS_IS_MY_ADMIN_SEATSIO_secretKey>')
let subaccountInfo = await clientAdmin.subaccounts.retrieve(seatsioid);
let secretKey = subaccountInfo.secretKey
let clientDesignerKey = subaccountInfo.designerKey
let charts = []
let clientUser = new SeatsioClient(secretKey)
for await(let chart of clientUser.charts.listAll()){
charts.push(`chart":"${chart.key}`)
}
let couple = {
charts,
clientDesignerKey
}
res.send(couple)
})
})
Thank you
save time and skip to answer
What's wrong with my axios 'GET' this.props.profile.id
logs into the console correctly from react-redux-firebase
, but that's another story.
...
const response = await axios.get(
"https://us-central1-thumbprint-1c31n.cloudfunctions/listCharts",
{ 'seatsioid': this.props.profile.id }
);
Or cloud functions header?
const functions = require('firebase-functions');
//const admin = require('firebase-admin');
const { SeatsioClient } = require('seatsio')
const cors = require('cors')
//admin.initializeApp(functions.config().firebase);
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google./docs/functions/write-firebase-functions
//
exports.listCharts = functions.https.onRequest(async(req, res) => {
cors(req, res, async () => {
res.set('Access-Control-Allow-Origin', 'https://1c31n.csb.app');
//res.set('Access-Control-Allow-Credentials', 'true');
res.set('Access-Control-Allow-Methods', 'GET');
res.set('Access-Control-Allow-Headers', 'Content-Type', 'X-Requested-Width', 'Accept', 'seatsioid')
res.set('Access-Control-Max-Age', '60');
var allowedOrigins = ['https://1c31n.csb.app', 'https://1c31n.codesandbox.io'];
var origin = req.headers.origin;
if(allowedOrigins.indexOf(origin) > -1){}
const seatsioid = **req.get('seatsioid')
let clientAdmin = new SeatsioClient('<THIS_IS_MY_ADMIN_SEATSIO_secretKey>')
let subaccountInfo = await clientAdmin.subaccounts.retrieve(seatsioid);
let secretKey = subaccountInfo.secretKey
let clientDesignerKey = subaccountInfo.designerKey
let charts = []
let clientUser = new SeatsioClient(secretKey)
for await(let chart of clientUser.charts.listAll()){
charts.push(`chart":"${chart.key}`)
}
let couple = {
charts,
clientDesignerKey
}
res.send(couple)
})
})
Thank you
Share Improve this question edited Dec 3, 2019 at 16:18 Nick Carducci for Carface Bank asked Nov 12, 2019 at 11:41 Nick Carducci for Carface BankNick Carducci for Carface Bank 5315 silver badges17 bronze badges2 Answers
Reset to default 7This took dayssss for me. hopefully this helps readers
express' cors middleware : https://expressjs./en/resources/middleware/cors.html
Google Cloud Function abstraction for cors middleware : https://cloud.google./functions/docs/writing/http
Call GCFunctions thru http (for fetch) : https://firebase.google./docs/functions/http-events
React fetch(URL, {})
async ponentDidMount() {
//console.log(this.props.profile)
//this.setState({ posterOptions: this.props.profile.pages.unshift('post as myself')})
//const seatsioid = this.props.profile.id;
await fetch(
"https://us-central1-foldername.cloudfunctions/function",
{
method: "POST",
credentials: "include",
headers: {
"Content-Type": "Application/JSON",
"Access-Control-Request-Method": "POST"
},
body: JSON.stringify({ seatsioid: this.props.profile.id }),
maxAge: 3600
//"mode": "cors",
}
)
.then(response => response.json())
.then(body => {
console.log(body);
//const reader = response.body.getReader()
//reader.read().then(value => {
//console.log(value);
this.setState({
allCharts: body.couple[0].charts,
secretKey: body.couple[0].secretKey
});
})
.catch(err => console.log(err));
}
Google Cloud Function
const functions = require("firebase-functions");
const { SeatsioClient } = require("seatsio");
const cors = require("cors")({
origin: true,
allowedHeaders: [
"Access-Control-Allow-Origin",
"Access-Control-Allow-Methods",
"Content-Type",
"Origin",
"X-Requested-With",
"Accept"
],
methods: ["POST", "OPTIONS"],
credentials: true
});
exports.listCharts = functions.https.onRequest((req, res) => {
// Google Cloud Function res.methods
res.set("Access-Control-Allow-Headers", "Content-Type");
res.set("Content-Type", "Application/JSON");
// CORS-enabled req.methods, res.methods
return cors(req, res, async () => {
res.set("Content-Type", "Application/JSON");
var origin = req.get("Origin");
var allowedOrigins = [
"https://www.yoursite.blah",
"https://yoursite2.blah"
];
if (allowedOrigins.indexOf(origin) > -1) {
// Origin Allowed!!
res.set("Access-Control-Allow-Origin", origin);
if (req.method === "OPTIONS") {
// Method accepted for next request
res.set("Access-Control-Allow-Methods", "POST");
//SEND or end
return res.status(200).send({});
} else {
// After req.method === 'OPTIONS' set ["Access-Control-Allow-Methods": "POST"]
// req.method === 'POST' with req.body.{name} => res.body.{name}
// req.method === 'PUT' with req.body.{name}, no res.body.{name}
let couple = [];
if (req.body.seatsioid) {
const seatsioid = req.body.seatsioid;
let clientAdmin = new SeatsioClient("YOUR_ADMIN_KEY");
let subaccountInfo = await clientAdmin.subaccounts.retrieve(seatsioid);
let secretKey = subaccountInfo.secretKey;
let charts = [];
let clientUser = new SeatsioClient(secretKey);
for await (let chart of clientUser.charts.listAll()) {
charts.push(`chart":"${chart.key}`);
}
couple = [
{
charts: charts,
secretKey: secretKey
}
];
//SEND or end
res.status(200).send({ couple });
} else {
//SEND or end
res.status(400).send("No seatsioid defined!");
}
}
} else {
//Origin Bad!!
//SEND or end
return res.status(400).send("no access for this origin");
}
});
});
Someone with 1500 rep points should maybe add the seatsio tag in replace of reactjs or javascript for their other users, thanks
Normally, in the Cloud Functions index.js
file, you should declare cors
as follows:
const cors = require('cors')({
origin: true
});
This is the way to allow current request origin, or, in other words "to reflect the request origin", see https://expressjs./en/resources/middleware/cors.html
Also see the Firebase documentation: https://firebase.google./docs/functions/http-events
本文标签: javascriptReact Fetch Google Cloud Function http cors reqmethod OPTIONS POSTStack Overflow
版权声明:本文标题:javascript - React Fetch Google Cloud Function http cors req.method OPTIONS, POST - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744071063a2528491.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论