Ad

Why Won't OnCall Function Return Data To The Client?

I am trying to build a simple web app. The user enters a email and a onCall cloud function retrieves the UID associated with that email and sends it back to the client. However, when I run this, it works in the server log, but returns null to the client. Why?

Cloud Function:

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);
        });
});

Client Code:

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);
            // ...
                })
});

Console Logs:


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


Ad

Answer

You need to return the actual getUserByEmail Promise:

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

Read more here: https://firebase.google.com/docs/functions/terminate-functions#how_promises_work_with_functions

Ad
source: stackoverflow.com
Ad