FlutterFlow Cloud Functions: No returned value when calling Firestore from inside the function

Database & APIs

Hey guys,

I need to check if some user exists in the "users" table by his UID. This table contains sensitive data, so it is protected and one can't directly "read" any of its records, only the authenticated user related to the specific record can get the data. So I should use a cloud function, since it's being run under a kind of "superuser" and dismiss any access checks in Firestore.

Here is my function code:

exports.checkUserExists = functions.https.onCall( (data) => {
  const {uid} = data;
    admin.firestore().collection("users").doc(uid).get().then(function (doc) {
      if (doc.exists) {
        console.log('Found');
        return 'Found';
      } else {
        console.log('Not found');
        return 'Not found';
      }
    }).catch(function (err) {
      console.log(err);
      return 'Error: ' + err;
    });
});

When I run this function and make an alert (informational dialog) with the result - it displays nothing. The raw result body is null.

It seems the code doesn't wait for Firestore get() request to be executed so the function immediately returns nothing... Any thoughts on that?

What have you tried so far?

I've tried many approaches, including adding async-await, returning a promise... No results.

If I change my cloud function to ...

exports.checkUserExists = functions.https.onCall( (data) => {
  const {uid} = data;
  return uid;
});

... everything works fine, it returns the uid parameter correctly. Obviously because there is no need to wait for a database call

Did you check FlutterFlow's Documentation for this topic?
Yes
1
4 replies