javascript - onCall firebase 函数上 firebase V9 中的 CORS 策略错误

标签 javascript node.js firebase google-cloud-functions

我遇到的问题是,只要我想调用我的 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/

相关文章:

javascript - 如何使用 Promise 链接 Postgres 查询,然后在 NodeJS 中获取请求

javascript - 如何在 package.json 文件中指定项目 npm 注册表

javascript - Ember 如何将 LinkView 与 View 助手一起使用?

node.js - Nginx 反向代理映射不重定向

javascript - 时间戳1小时内的Mongo Node查询

ios - Firebase setValue 在 IOS 10、Swift 3 中不起作用

android - Firebase 获取最后插入项目的 key

java - 如何从 Firebase 存储获取 URL getDownloadURL

javascript - 按索引更新数组中的 JSON 键值

javascript - Facebook Messenger 机器人问题