0

I am attempting to call a Firebase Function from my Flutter project. In my pubspec.yaml file I have imported the functions import: cloud_functions: ^0.4.0+2. I have also set up all the firebase configurations as I have a working connection to the firestore database from my app. I then created and deployed a Firebase Function in javascript as follows:

exports.addEventToCalendar = functions.https.onRequest((request, response) =>
const eventData = {
    eventName: request.body.eventName,
    description: request.body.description,
    startTime: request.body.startTime,
    endTime: request.body.endTime
    };
//more code using these variables
}

I have successfully deployed this function to Firebase and have successfully ran and tested it in the Google Developer Console with the following JSON data:

{
"eventName": "Firebase Event",
"description": "This is a sample description",
"startTime": "2019-07-18T10:00:00",
"endTime": "2019-07-18T14:00:00"
}

So I know this function works.

Now when I try to call this on my Flutter App using this method:

void _addToCal() async {
  try {
    final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(
      functionName: 'addEventToCalendar',
    );
    dynamic resp = await callable.call(<String, dynamic>{
      "eventName": "Flutter Event",
      "description": "This is a sample description",
      "startTime": "2019-07-15T10:00:00",
      "endTime": "2019-07-15T14:00:00"
    });
  } catch (e, s) {
    print(e);
    print(s);
  }
}

I get an Instance of 'CloudFunctionsException' error.

On the Firebase side, I look into the logs of the function and see several errors including: TypeError: Cannot read property 'data' of undefined.

Thanks for any help as I am new to Firebase and Cloud Functions.

2 Answers 2

4

You're invoking a callable function from your Flutter code, but you're declaring a regular HTTPS triggered function in your index.js. Callable Cloud Functions are declared as shown here:

exports.addMessage = functions.https.onCall((data, context) => {
  // ...
});
Sign up to request clarification or add additional context in comments.

3 Comments

When I change it to this, my logs for the function in Firebase say 'Unhandled error TypeError: Cannot read property 'eventName' of undefined'?
Did you update the Cloud Functions to read its parameters from data and context as shown in the documentation I linked?
Thank you @Frank! This is the right answer. There's need to also add a return to the cloud function, as the response object is not available.
2

Try getting the data from your function in this way:

const { body } = req;
const eventName = req.query.eventName || body.eventName || (body.data && body.data.eventName);
const description = req.query.description || body.description || (body.data && body.data.description);
const startTime = req.query.startTime || body.startTime || (body.data && body.data.startTime);
const endTime = req.query.endTime || body.endTime || (body.data && body.data.endTime);

const eventData = {
    eventName: eventName,
    description: description,
    startTime: startTime,
    endTime: endTime
    };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.