[英]Why won't onCall function return data to the client?
我正在嘗試構建一個簡單的Web應用程序。 用戶輸入電子郵件,並且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
承諾:
return admin.auth().getUserByEmail(email).then( //...
在此處了解更多信息: https : //firebase.google.com/docs/functions/terminate-functions#how_promises_work_with_functions
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.