0

I have a firestore project in which there are names of towers as following

"society name"
      |
      |-----"tower name"
      |
      |-----"tower name"
and so on

i want to create a cloud function which takes the name of society as input and returns the name of all the tower in that society and they are further populated into my spinner in the app.

there were reference found in this context so i am asking it here.

2 Answers2

0

Checking the documentation I have found how to use Firestore from cloud Functions. You should use that guide to start. In this other post you will see how to get a document's name.

The result should be something like this:

const Firestore = require('@google-cloud/firestore');

PROJECT_ID = '[project-id]'

const firestore = new Firestore({
    projectId: PROJECT_ID,
    timestampsInSnapshots: true,
});

exports.main = (req, res) => {
    const data = (req.body) || {};

    return firestore.collection(data.society).get()
        .then((querySnapshot) => {
            var ids = [];
            querySnapshot.forEach(doc => {
                ids.push(doc.id);
            });
            return res.status(200).send(ids);
        })
        .catch(error => {
            return res.status(404).send(error);
        });
};
Ajordat
  • 1,320
  • 2
  • 7
  • 19
  • thanks for the help regarding the cloud function. but how do i populate it further in the spinner? The question i asked originally referred to this problem also – Nimesh Garg May 23 '20 at 13:49
  • I edited your question removing the issue about spinner as that was a completely different thing that should be handled in another question, [as it has already been done](https://stackoverflow.com/a/11920785/10810527). Regardless, I believe that you should still think about @mobigeek's suggestion and call Firestore directly from your code instead of using a Cloud Function. – Ajordat May 25 '20 at 10:35
0

Since you are writing this as a Java app (comment in OP), why not code directly against Firestore rather than go through a Cloud Function?

If you decide to still go after using Cloud Functions, here are things you will want to consider that Firestore would give you:

  • how to serialize/deserialize the data between your app and Cloud Function
  • how to handle security (Firestore has security rules built right in)
  • authentication (if needed...which it should be :) )

And you wouldn't have the ability to "listen" to the data, meaning being able to get live updates to your user via the UI.

Greg Fenton
  • 1,233
  • 9
  • 15