Returning a map in Firebase cloud function is null Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Returning a map in Firebase cloud function is null without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am trying to return a set of values in a Firebase cloud onCall function, but when I read the value in my Android app, part of the data is missing.

Here is the cloud function:

exports.getPhotoUrls = functions.https.onCall(async (data, context) => {

  let usersAndPhotos = new Map();
  usersAndPhotos.set("bob", "2345");
  usersAndPhotos.set("sam", "345");

  const returnVal = {
    payload: usersAndPhotos,
    status: 200,
    message: "Success"
  }
  console.log("ReturnVal: " );
  console.log(returnVal)

  return returnVal
})

When I check in the Firebase functions log, I see that the returned value is:

{ payload: Map { 'bob' => '2345', 'sam' => '345' },
  status: 200,
  message: 'Success' }

However, when the function returns to my Android app, the payload Map is missing:

enter image description here

Why is my payload Map data disappearing?

Answer

This Map right here:

let usersAndPhotos = new Map();

is an ES6 Map type object. It can’t be effectively serialized by Cloud Functions callables. If you want to send a map back to the client, what you should use instead is use a plain old JavaScript object, which has a clear and direct serialization to JSON (which is what’s being used as the actual payload):

let usersAndPhotos = {
  bob: "2345",
  sam: "345"
}
We are here to answer your question about Returning a map in Firebase cloud function is null - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji