1
votes

Getting error when trying to access my cloud function:

{"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}

If the client trigger is invoked, but the request is in the wrong format, such as not being JSON, having invalid fields, or missing the data field, the request is rejected with 400 Bad Request, with an error code of INVALID_ARGUMENT.

Firebase Documentation: Firebase Error Documentation

index.js in functions directory

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

var admin = require("firebase-admin");
var serviceAccount = require("./fir-email-b4c1f-firebase-adminsdk-xj7ug-d01651ffc9");


admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://fir-email-b4c1f.firebaseio.com"
});



    exports.getUserByEmail = functions.https.onCall((data, context) => {

const email = data.email;

return admin.auth().getUserByEmail(email)
    .then(userRecord => {
    console.log('Successfully fetched user data:', userRecord.toJSON());
    const userData = userRecord.toJSON();
    return { userData: userData };
})

});

authentication.js calling function in project main directory

    function retrieveUserData() {

    var emailString = "test@gmail.com";
    var userEmail = firebase.functions().httpsCallable('getUserByEmail');

    userEmail({email: emailString}).then(function(result) {
        // Read result of the Cloud Function.
        var emailResult = result.data.text;
        console.log('Successfully fetched user data:', emailResult.toJSON());


    })

}

Not fully understanding the error. I assume I am missing an argument based on the documentation. Can someone guide me in the right direction.

1

1 Answers

1
votes

Yes, it seems you are indeed missing an argument. Your first parameter (data) for the getUserByEmail() function is supposed to be an object that contains the property: email from this line: const from = data.sender;. But when calling it you pass in an object with only a text property. Which means, when your cloud function runs, data.email is undefined.

Probably, you mean to call the function like this:

userEmail({email: emailString}).then(function(result) {
        // Read result of the Cloud Function.
}

UPDATE: On the second issue: Your cloud function returns an object with just a lone property: userData, so if you want to log the returned value in your output, you'll need to replace

var emailResult = result.data.text;
console.log('Successfully fetched user data:', emailResult.toJSON());

with this:

var emailResult = result.data.userData;
console.log('Successfully fetched user data:', emailResult);

All the extra fields: result.data.text do not exist on the object returned. And you already converted your firebase response to JSON in your cloud function. You can't be doing that again on the client