Firestore + cloud functions: How to read from anot

2020-08-15 10:46发布

问题:

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!

回答1:

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);
            });
    });