I'm trying to run official quickstart example of Firebase Cloud Functions for Unity engine. The parameter data (dictionary of objects) passed when calling function from unity is always empty. I tried different Firebase SDK versions, but the problem is always there. I have no idea what the problem could be, please help.
Here is the code in Unity:
void Start()
{
StartCoroutine(AddNumbers(7, 7));
}
protected IEnumerator AddNumbers(int firstNumber, int secondNumber) {
var functions = FirebaseFunctions.GetInstance(FirebaseApp.DefaultInstance);
var func = functions.GetHttpsCallableFromURL("https://getnumbers-dxhi3lnaew-uc.a.run.app");
var data = new Dictionary<string, object>();
data["firstNumber"] = firstNumber;
data["secondNumber"] = secondNumber;
var task = func.CallAsync(data).ContinueWithOnMainThread((callTask) => {
if (callTask.IsFaulted) {
// The function unexpectedly failed.
Debug.Log("FAILED!");
Debug.Log(String.Format(" Error: {0}", callTask.Exception));
return;
}
// The function succeeded.
var result = (IDictionary)callTask.Result.Data;
Debug.Log(String.Format("AddNumbers: {0}", result["operationResult"]));
});
yield return new WaitUntil(() => task.IsCompleted);
}
Cloud function code:
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
// Adds two numbers to each other.
exports.getNumbers = functions.https.onCall((data) => {
// Numbers passed from the client.
const firstNumber = data.firstNumber;
const secondNumber = data.secondNumber;
// Checking that attributes are present and are numbers.
if (Number.isFinite(firstNumber) || !Number.isFinite(secondNumber)) {
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('invalid-argument', '!The function ' +
'must be called with two arguments "firstNumber" and "secondNumber" ' +
'which must both be numbers.');
}
// returning result.
return {
firstNumber: firstNumber,
secondNumber: secondNumber,
operator: '+',
operationResult: firstNumber + secondNumber,
};
});
The argument to the Cloud Function is actually the entire request
object, and the data you pass in the C# code is in a data
property in there.
So:
exports.getNumbers = functions.https.onCall((request) => {
// Numbers passed from the client.
const firstNumber = request.data.firstNumber;
const secondNumber = request.data.secondNumber;
...
Also see the Firebase documentation on callable functions, which has example showing this.