Firebase的可调用函数给我带来了CORS问题 [英] Firebase callable functions giving me CORS issue

查看:96
本文介绍了Firebase的可调用函数给我带来了CORS问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试可调用函数,以避免手动处理cors,但到目前为止没有任何作用

I've been trying callable functions to avoid handling cors manually but so far nothing is working

我读过类似的问题 Firebase可调用函数+ CORS

区别在于该家伙具有无访问权限允许原点"问题.我的是对飞行前的反应...问题

the difference is the that dude has 'no access control allow origin' issue. Mine is response to preflight ... issue

我想请人帮忙.真令人沮丧.

I'd like to request for a guidance people. It's frustrating argh..

我正在执行Blaze计划

I'm on Blaze plan

到目前为止,我一直在尝试仅在本地运行功能.我根本没有部署该功能.

So far i've been trying only to run the functions Locally. i have not deployed the function at all.

这是我的功能代码

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();


exports.createOrder = functions.https.onCall((data, context) => {
return new Promise((resolve, reject) => {
    resolve(data)
        .then(result => {
            return result;
        })
        .catch(err => {
            console.error(err.code);
            throw new functions.https.HttpsError('unknown', err.message);
        });
});

}); 这是我的客户代码

}); this is my client code

export const createOrder = order => {
    functions
        .httpsCallable('createOrder')({ data: order })
        .then(result => {
            console.log('result create order is', result);

            return result;
        });
};

功能package.json

functions package.json

{
    "name": "functions",
    "description": "Cloud Functions for Firebase",
    "scripts": {
        "serve": "firebase serve --only functions",
        "shell": "firebase functions:shell",
        "start": "npm run shell",
        "deploy": "firebase deploy --only functions",
        "logs": "firebase functions:log"
    },
    "dependencies": {
        "@sendgrid/mail": "^6.4.0",
        "axios": "^0.19.0",
        "cors": "^2.8.5",
        "firebase": "^6.3.1",
        "firebase-admin": "^8.2.0",
        "firebase-functions": "^3.1.0",
        "firebase-functions-test": "^0.1.6"
    },
    "engines": {
        "node": "8"
    },
    "private": true
}

firebase config.json

firebase config.json

"firebase": {
            "apiKey": "AIzaSy*****",
            "authDomain": "jokii-test.firebaseapp.com",
            "databaseURL": "https://jokii-test.firebaseio.com",
            "projectId": "jokii-test",
            "storageBucket": "jokii-test.appspot.com",
            "messagingSenderId": "104544.....",
            "appId": "1:10454485....."
        }

Firebase SDK ="firebase":"^ 6.3.1",

Firebase SDK = "firebase": "^6.3.1",

推荐答案

客户端SDK不知道如何访问尚未部署的功能.注意,在错误输出中,它尝试访问的URL是完全部署的函数的URL.当前无法使其覆盖URL来访问您在本地主机上本地运行的URL.

The client SDK doesn't know how to access a function that hasn't been deployed. Notice in the error output that the URL it's trying to access is the one for your fully deployed function. It's currently not possible to have it override the URL to access one you're running locally on localhost.

如果要在本地调用可调用函数,则需要实现遵循可调用函数的协议规范.而且,正如您所看到的,这将是相当多的工作,因为客户端SDK正在为您管理许多HTTP协议.

If you want to invoke your callable function locally, you'll need to implement a client that follows the protocol specification for callable functions. And, as you can see, that's going to be a fair amount of work, because the client SDK is managing a lot of the HTTP protocol work for you.

这篇关于Firebase的可调用函数给我带来了CORS问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆