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

时间:2019-08-15 11:11:43

标签: javascript firebase google-cloud-functions

我正在尝试构建一个简单的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


1 个答案:

答案 0 :(得分:2)

您需要返回实际的getUserByEmail承诺:

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

在此处了解更多信息:https://firebase.google.com/docs/functions/terminate-functions#how_promises_work_with_functions