Firebase Cloud Functions: “status”:“INVALID_ARGUMENT”

馋奶兔 提交于 2020-06-09 05:49:47

问题


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.


回答1:


You're comparing a callable function using onCall to an HTTP function using onRequest. Callable functions are implemented very differently than HTTP functions. I suggest reading over the documentation I linked in order to better understand the difference. The main point is that callable functions follow a specific protocol, and that any client access must follow that protocol, or possibly receive the error that you show here. The best implementation of that protocol is the client SDK provided by Firebase - you should use that to invoke the function.

If you want a normal HTTP function to be invoked using the usual HTTP libraries, then don't use a callable at all. You can pass authentication information to an HTTP function manually using an ID token then verify it using the Firebase Admin SDK. The documentation link has examples.




回答2:


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.



来源:https://stackoverflow.com/questions/62091410/firebase-cloud-functions-statusinvalid-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!