gpt4 book ai didi

google-cloud-functions - 为什么一次可调用客户端调用会调用两次 onCall firebase 函数?

转载 作者:行者123 更新时间:2023-12-05 02:08:56 24 4
gpt4 key购买 nike

我的客户端代码仅调用该函数一次,但在服务器日志中它显示了两次单独的调用。每对中的第一个似乎并没有真正处理我的服务器端代码 - 这是预期的行为吗?

onCall 函数:

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

exports.test = functions
.region('asia-northeast1')
.https
.onCall(async (data, context) => {

console.log('function test: start')

if (!context.auth) {
console.log('function test: throwing permission-denied');
throw new functions.https.HttpsError('permission-denied', 'Mysterious');
}

console.log('function test: no error, user authorised');

});

客户端 HTML:

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Test</title>

<script defer src="/__/firebase/7.7.0/firebase-app.js"></script>
<script defer src="/__/firebase/7.7.0/firebase-auth.js"></script>
<script defer src="/__/firebase/7.7.0/firebase-firestore.js"></script>
<script defer src="/__/firebase/7.7.0/firebase-messaging.js"></script>
<script defer src="/__/firebase/7.7.0/firebase-storage.js"></script>
<script defer src="/__/firebase/7.7.0/firebase-functions.js"></script>

<script defer src="/__/firebase/init.js"></script>

</head>

<body>
<script src="app.js"></script>

<button onclick="login()">
Login with Google
</button>

<button onclick="logout()">
Log out
</button>

<button onclick="test()">
test
</button>

</body>

</html>

客户端JS:

var user, app, functions, callableTest;

document.addEventListener("DOMContentLoaded", async event => {
app = firebase.app();
functions = app.functions('asia-northeast1');
callableTest = functions.httpsCallable('test');
user = await firebase.auth().currentUser;
console.log('user: ' + user);
});

async function login() {
user = await firebase.auth().signInWithPopup(new firebase.auth.GoogleAuthProvider());
}

async function logout() {
await firebase.auth().signOut();
user = null;
}

async function test() {
console.log('calling test function');
await callableTest();
console.log('returned from test function');
}

这些是[测试]的日志点击预登录,然后[登录..],然后再次[测试]:

Image of logs

D 2020-02-22T04:08:27.209014265Z test 7whrcrhllsa8 Function execution took 5 ms, finished with status code: 403 test 7whrcrhllsa8 
I 2020-02-22T04:08:27.208Z test 7whrcrhllsa8 function test: throwing permission-denied test 7whrcrhllsa8
I 2020-02-22T04:08:27.208Z test 7whrcrhllsa8 function test: start test 7whrcrhllsa8
D 2020-02-22T04:08:27.205019518Z test 7whrcrhllsa8 Function execution started test 7whrcrhllsa8
D 2020-02-22T04:08:27.072797567Z test 7whrdta870ib Function execution took 42 ms, finished with status code: 204 test 7whrdta870ib
D 2020-02-22T04:08:27.031343424Z test 7whrdta870ib Function execution started test 7whrdta870ib
D 2020-02-22T04:07:39.540689974Z test 7whryp7xojaf Function execution took 87 ms, finished with status code: 403 test 7whryp7xojaf

最佳答案

状态代码为 204 的调用是 CORS pre-flight request 的结果当请求来自网页时(在您的情况下)。可调用函数句柄 CORS自动安全,因此所有这一切都无需您干预即可发生。

这记录在 protocol spec for callable functions 中.

关于google-cloud-functions - 为什么一次可调用客户端调用会调用两次 onCall firebase 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60349364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com