简体   繁体   English

CORS onCall firebase V9 中的策略错误 firebase function

[英]CORS Policy error in firebase V9 on an onCall firebase function

I have the problem that as soon as I want to make a call to my firebase functions I get a Cors policy Error.我遇到的问题是,一旦我想调用我的 firebase 函数,我就会收到 Cors 策略错误。 I have already read multiple questions about that but I couldn't find one that solves the issue in the Firebase version 9. The call syntax is different in v9.我已经阅读了多个关于此的问题,但在 Firebase 版本 9 中找不到解决问题的问题。调用语法在 v9 中有所不同。 And I can't figure out how to solve it.而且我不知道如何解决它。

Error:错误:

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 Initialization: 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()

Function Call: Function 电话:

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 Functions: Firebase 职能:

const functions = require("firebase-functions");

exports.sendRequest = functions.https.onCall(async(data, context) => {
    console.log("ok")
});

Note: (I am using the emulator at the moment) Note: (I tried to change the location of the server but I am not sure how to do that in the emulator and in the frontend in v9)注意:(我现在正在使用模拟器)注意:(我试图更改服务器的位置,但我不确定如何在模拟器和 v9 的前端中执行此操作)

Thank you for your time!感谢您的时间!

Try this尝试这个

import { connectFunctionsEmulator } from "firebase/functions";

connectFunctionsEmulator(functions, "localhost", 5001);

I couldn't find a better way than just downgrading to firebase@8.10.0 and using the following code:除了降级到 firebase@8.10.0 并使用以下代码,我找不到更好的方法:

Firebase init: Firebase 初始化:

import firebase from "firebase/app";
import "firebase/functions"

const firebaseConfig = {
    apiKey: "XXXX",
    authDomain: "XXXX",
    projectId: "XXXX",
    storageBucket: "XXXX",
    messagingSenderId: "XXXX",
    appId: "XXXX",
    measurementId: "XXXX"
};

// Initialize Firebase
firebase.initializeApp(firebaseConfig);


firebase.functions().useEmulator("localhost", 5001);


export default firebase

Funcion Call:函数调用:

import firebase from "./firebase.js";


function sendRequest(e) {
  const sendRequest = firebase.functions().httpsCallable('sendRequest');
  sendRequest()
}

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

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