Skip to content
Advertisement

Firebase cloud functions: can’t extract parameters when invoking a callable function

I’m using the Flutter SDK to call a cloud function. I add a parameter someField but on the cloud side, it cannot be retrieved and is always undefined. This is how I call the cloud function

static Future<void> doSomething() async {
  await Firebase.initializeApp();
  final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(
    functionName: 'myFunction',
  );
  dynamic resp = await callable.call(
    <String, dynamic>{
      'someField': 'Hurray!',
    },
  );
}

The cloud function is written as follows

exports.myFunction = functions.https.onRequest((req, res) => {
  cors(req, res, () => {
      const myParameter = req.body.someField; // is always Undefined
      // also tried
      // req.query.someField
      // req.params.someField
      doSomething(myParamter)
  })
});

Advertisement

Answer

You’re mixing up callable functions on the client app with HTTPS functions on the backend. Please review the documentation to understand the difference between them. If you want to use the Firebase SDK on the client to invoke a Cloud Functions, you should declare that using onCall instead of onRequest. When you write a callable function with onCall, you will have access to the input arguments via the first parameter delivered to the callback.

exports.myFunction = functions.https.onCall((data, context) => {
  // data.someField should be populated from your client request
});
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement