为什么一个Firebase函数没有通过cors预检,而另一个却没有通过?

时间:2020-05-07 13:35:27

标签: angular firebase google-cloud-functions

所以我有两个以相同方式创建的函数(但是显然做不同的事情):

// I have deleted the content, as it is not relevant for the issue. names and everything are the same though

// Disclaimer:
// The FireFunctions element comes from a shared module for backend/frontend and looks like this:

export const FireFunctions = {
    users: {
        generateFacebookMessengerToken: 'generateFacebookMessengerToken',
        getOrderDataFromTokenAndUid: 'getOrderDataFromTokenAndUid'
    }
};

// Function 1
exports[FireFunctions.users.generateFacebookMessengerToken] = functions.https.onCall(async (data, context) => {
    console.log('stuff 1');
});

// Function 2
exports[FireFunctions.users.getOrderDataFromTokenAndUid] = functions.https.onCall(async (data, context) => {
    console.log('stuff 2');
});

// Function 2 (test)
// I have also have tested it with a static name instead of using my shared module but it does make no difference
export const testSomeStuff = functions.https.onCall(async (data, context) => {
    console.log('stuff 2');
});

所以当我想像这样从我的前端调用函数时,问题就出现了:

// Works
const response = await this.functions
    .httpsCallable<void, void>(FireFunctions.users.generateFacebookMessengerToken)().toPromise();

// Does not work
const response = await this.functions
    .httpsCallable<void, void>(FireFunctions.users.getOrderDataFromTokenAndUid)().toPromise();

// Does also not work with a static name
const response = await this.functions
    .httpsCallable<void, void>('testSomeStuff')().toPromise();

这三个版本都显示在功能面板中,但是只有第一个版本在被调用时才会被记录。 其他两个出现以下错误:

访问地址为 'https://us-central1-someproject.cloudfunctions.net/testSomeStuff' 来自来源“ http://localhost:8100”的信息已被CORS政策阻止: 对预检请求的响应未通过访问控制检查:否 请求中出现“ Access-Control-Allow-Origin”标头 资源。如果不透明的响应满足您的需求,请设置请求的 模式设置为“ no-cors”,以在禁用CORS的情况下获取资源。

这些功能之间的唯一区别是,第一个功能存在几个月,而第二个功能是我最近创建的,但从未使用过。

0 个答案:

没有答案