我遇到的问题是,只要我想调用我的 firebase 函数,就会收到 Cors 策略错误。我已经阅读了多个关于此的问题,但我在 Firebase 版本 9 中找不到解决该问题的问题。v9 中的调用语法不同。而且我不知道如何解决它。
错误:
Access to fetch at 'https://us-central1-mmonitor-19efd.cloudfunctions.net/sendRequest' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Firebase 初始化:import { initializeApp } from 'firebase/app';
import { getFunctions } from 'firebase/functions';
const firebaseConfig = {
apiKey: "XXXX",
authDomain: "XXXX",
projectId: "XXXX",
storageBucket: "XXXX",
messagingSenderId: "XXXX",
appId: "XXXX",
measurementId: "XXXX"
};
export const firebaseApp = initializeApp(firebaseConfig);
export const functions = getFunctions()
函数调用:import { firebaseApp, functions } from "./firebase"
import { httpsCallable } from 'firebase/functions';
function sendRequest(e) {
const sendRequest = httpsCallable(functions, 'sendRequest');
sendRequest()
}
function App() {
return (
<button onClick={sendRequest}>send Request</button>
);
}
export default App;
Firebase 功能:const functions = require("firebase-functions");
exports.sendRequest = functions.https.onCall(async(data, context) => {
console.log("ok")
});
注意:(我目前正在使用模拟器)注意:(我试图更改服务器的位置,但我不确定如何在模拟器和 v9 的前端中执行此操作)
感谢您的时间!
最佳答案
尝试这个
import { connectFunctionsEmulator } from "firebase/functions";
connectFunctionsEmulator(functions, "localhost", 5001);
关于javascript - onCall firebase 函数上 firebase V9 中的 CORS 策略错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69106918/