gpt4 book ai didi

javascript - 为什么onCall函数不会返回数据给客户端?

转载 作者:行者123 更新时间:2023-11-30 13:54:50 25 4
gpt4 key购买 nike

我正在尝试构建一个简单的网络应用程序。用户输入电子邮件,onCall 云功能检索与该电子邮件关联的 UID 并将其发送回客户端。但是,当我运行它时,它在服务器日志中工作,但向客户端返回 null。为什么?

云函数:

exports.wheresmyUID = functions.https.onCall((data, context) => {
const email = data.email;
// if (email.length === 0) {
// throw new functions.https.HttpsError('invalid-argument', 'You must enter a valid email.');
// }
admin.auth().getUserByEmail(email)
.then((userRecord) => {
var myUID = userRecord.uid;
console.log("Successfully fetched user data: " + myUID);
return myUID;
})
.catch((error) => {
throw new functions.https.HttpsError('unknown', error.message, error);
});
});

客户端代码:

subBTN.addEventListener("click", e => {

//VARIABLES OR SOMETHING
var myEmail = document.getElementById("myEmail");
var subBTN = document.getElementById("subBTN");
var infoZone = document.getElementById("infoZone");

var email = myEmail.value;


var wheresmyUID = firebase.functions().httpsCallable('wheresmyUID');
wheresmyUID({email}).then(function(result) {
// Read result of the Cloud Function.
console.log(result);
console.log("Data: " + result);
var nowUID = result.data.text;
infoZone.innerHTML = "<p>" + nowUID + "</p>";
}).catch(function(error) {
// Getting the Error details.
console.log("Something has gone wrong boss! " + error);
var code = error.code;
var message = error.message;
var details = error.details;
console.log("Code " + code);
console.log("Message " + message);
console.log("Details " + details);
// ...
})
});

控制台日志:


Data: [object Object]
callmyUID.js:39 Something has gone wrong boss! TypeError: Cannot read property 'text' of null
callmyUID.js:43 Code undefined
callmyUID.js:44 Message Cannot read property 'text' of null
callmyUID.js:45 Details undefined


最佳答案

您需要返回实际的 getUserByEmail Promise:

return admin.auth().getUserByEmail(email).then( //...

在这里阅读更多:https://firebase.google.com/docs/functions/terminate-functions#how_promises_work_with_functions

关于javascript - 为什么onCall函数不会返回数据给客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57508788/

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