Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore + cloud functions: How to read from another document

I'm trying to write a Google cloud function that reads from another document. (Other document = not the document that triggered the cloud function.)

It's a bit of a treasure hunt to figure out how to do such a simple thing.

  1. The cloud functions documentation seems to suggest to look at the admin SDK: "You can make Cloud Firestore changes via the DeltaDocumentSnapshot interface or via the Admin SDK."

    https://firebase.google.com/docs/functions/firestore-events

  2. The Admin SDK suggest to write the following line of code to get a client. But oh no, it's not going to explain the client. It's going to send us off to a wild goose chase elsewhere in the documentation.

    var defaultFirestore = admin.firestore();

    "The default Firestore client if no app is provided or the Firestore client associated with the provided app."

    https://firebase.google.com/docs/reference/admin/node/admin.firestore

  3. That link resolves to a general overview page with no direct clue on figuring out the next thing.

    https://cloud.google.com/nodejs/docs/reference/firestore/0.10.x/

  4. Digging a big around, there is a promising class called FireStoreClient. It has a 'getDocument' method that seems promising. The parameter seems complicated. Rather than simply passing the path into the method, it seems to want an entire document/collection something as a parameter.

    https://cloud.google.com/nodejs/docs/reference/firestore/0.10.x/FirestoreClient#getDocument

    var formattedName = client.anyPathPath("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]"); client.getDocument({name: formattedName}).then(function(responses) { var response = responses[0]; // doThingsWith(response) })

So, I'm trying to combine all of this information into a Google cloud function that will read from another document.

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.updateLikeCount4 = functions.firestore
    .document('likes/{likeId}').onWrite((event) => {
        return admin.firestore()
            .getDocument('ruleSets/1234')
            .then(function(responses) {
                 var response = responses[0];
                 console.log('Here is the other document: ' + response);
             })
    });

That approach fails with:

admin.firestore.getDocument is not a function

I've also tried. admin.firestore.document, admin.firestore.doc, admin.firestore.collection, and many more. None of them seem to be a function.

All I want is to read from another Firestore document in my Google cloud function.

PS: They said the documentation is your friend. This documentation is a nightmare that follows the principle of scatter all the clues into the four directions of the wind!

like image 769
Thomas Fischer Avatar asked Nov 28 '17 00:11

Thomas Fischer


People also ask

How do I read a document on firestore?

To read a single document, we can use DocumentReference's get() method that returns a Task<DocumentSnapshot>, while reading multiple documents from a collection or Query, we can use Firestore Query's get() method that returns an object of type Task<QuerySnapshot>. Both methods read the data only once.

How do I query Subcollection in firestore?

You can either go to Firestore Databases > Indexes console and do it, or use the firebase cli. But the easiest option is to just let your code (that performs the query) run and firestore will automatically error out when an index is missing.


1 Answers

Thank you, @frank-van-puffelen.

This is the working solution:

exports.updateLikeCount = functions.firestore
    .document('likes/{likeId}').onWrite((event) => {
        return admin.firestore()
            .collection('ruleSets')
            .doc(1234)
            .get()
            .then(doc => {
                console.log('Got rule: ' + doc.data().name);
            });
    });
like image 82
Thomas Fischer Avatar answered Oct 18 '22 21:10

Thomas Fischer