使用HttpScalable()调用Firebase云函数时出现CORS错误

使用HttpScalable()调用Firebase云函数时出现CORS错误,firebase,cors,google-cloud-functions,Firebase,Cors,Google Cloud Functions,我正在尝试从React客户端调用Firebase云函数 我能够使用HTTP请求成功地调用这些函数(如上所述)。这需要在云功能中设置完整的Express应用程序 现在,我正试图使用httpscalable()(如上所述)直接从客户端调用云函数。与通过HTTP请求调用相比,此方法似乎有两个优点。然而,使用这种方法,我得到以下CORS错误: CORS策略已阻止从源“”获取“”的访问 我该怎么做?值得这么麻烦吗?这真的是首选的方式吗 以下是我的云功能: import * as functions fro

我正在尝试从React客户端调用Firebase云函数

  • 我能够使用HTTP请求成功地调用这些函数(如上所述)。这需要在云功能中设置完整的Express应用程序

  • 现在,我正试图使用
    httpscalable()
    (如上所述)直接从客户端调用云函数。与通过HTTP请求调用相比,此方法似乎有两个优点。然而,使用这种方法,我得到以下CORS错误:

  • CORS策略已阻止从源“”获取“”的访问

    我该怎么做?值得这么麻烦吗?这真的是首选的方式吗

    以下是我的云功能:

    import * as functions from 'firebase-functions';
    
    export const helloWorld = functions.https.onRequest((request, response) => {
        response.send('Hello from Firebase!');
    });
    
    以下是我的客户对它的称呼:

    const sayHello = async (): Promise<string> => {
        const helloWorld = firebase.functions().httpsCallable('helloWorld');
        const result = await helloWorld();
        return result.data;
    };
    
    const sayHello=async():Promise=>{
    const helloWorld=firebase.functions().httpscalable('helloWorld');
    const result=等待helloWorld();
    返回结果数据;
    };
    
    通过执行

    const helloWorld = firebase.functions().httpsCallable('helloWorld');
    const result = await helloWorld();
    
    您确实在调用,,但是通过如下定义被调用的函数

    functions.https.onRequest((request, response) => {})
    
    您正在定义一个不同的

    您应该将云函数定义为可调用函数,如下所示:

    export const helloWorld =  = functions.https.onCall((data, context) => {
      return { response: 'Hello from Firebase!' };
    });
    
    通过做

    const helloWorld = firebase.functions().httpsCallable('helloWorld');
    const result = await helloWorld();
    
    您确实在调用,,但是通过如下定义被调用的函数

    functions.https.onRequest((request, response) => {})
    
    您正在定义一个不同的

    您应该将云函数定义为可调用函数,如下所示:

    export const helloWorld =  = functions.https.onCall((data, context) => {
      return { response: 'Hello from Firebase!' };
    });
    

    非常感谢你,雷诺!您对可调用云函数与HTTPS云函数的解释比文档中的解释要清楚得多。我希望文档在这两部分之前有一个概述部分,解释两者的区别,并警告不要将两者混用。非常感谢你,雷诺!您对可调用云函数与HTTPS云函数的解释比文档中的解释要清楚得多。我希望文档在这两部分之前有一个概述部分,解释两者的区别,并警告不要将两者混用。