'FirebaseCloudFunctions only returns null

I am trying to get a response from a Firebase cloud function, but the only thing I get back is null. The console output shows that the fetch command works and outputs the correct data.

exports.create = functions.https.onCall((data, context) => {
fetch("https://mywebsite.com/x", {
    method: "POST",
    headers: {
        "Authorization": "Bearer MyKey",
        
    },
    body: JSON.stringify({"myData":data.myData})
}).then(
    function(response) {
      if (response.status !== 200) {
        console.log('Looks like there was a problem. Status Code: ' +
          response.status);
        return;
      }

      // Examine the text in the response
      response.json().then(function(data) {
        console.log(data);
        
        
      });
      return data.json();
    }
  )
  .catch(function(err) {
    console.log('Fetch Error :-S', err);
  });

});

I tried using Promise, I tried returning is as json and just as return data nothing seems to work. The result is always null

The code I use to call it in my flutter app:

HttpsCallable callable =
        FirebaseFunctions.instance.httpsCallable('create');
    final response = await callable.call(<String, dynamic>{
      'myData': 'data',
      
    }).then((value) => print(value.data));


Solution 1:[1]

You're not returning anything from the top-level code in your function, which means that Cloud Functions terminates the container when the final } executes, well before the fetch call ever completes.

To both allow the fetch call to complete and return a value to the caller, be sure to return from the top-level of your cde:

exports.create = functions.https.onCall((data, context) => {
  // ?
  return fetch("https://mywebsite.com/x", {
    ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Frank van Puffelen