I am locally testing a Firebase Cloud Function. When I call this function using the local URL http://localhost:5001/projectName/us-central1/functionName
as described here:
exports.createSession = functions.https.onRequest((_req, res) => {
res.status(200).send('TESTING');
});
the function works and it returns the string.
However, when I call this function:
exports.createSession = functions.https.onCall((data, context) => {
return 'TESTING';
});
It throws the error: {"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}
I'd like to use the latter function because I want to access the context.auth
object to check for user Firebase authentication.
I'm using Firebase CLI v8.4 and Node v10.20.
What am I missing from the second function to get it working? I am not calling it with any arguments as I do not need to.
The answer is that you can't call the .onCall
method in the same way as you can the onRequest
method, you have to call it from the SDK. If the data
argument is missing it will throw an error:
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.
So you have to call it with an argument even if you don't need to use the argument for anything.