title: Firestore + cloud functions: How to read from another document
body: <p>I'm trying to write a Google cloud function that reads from another document. (Other document = not the document that triggered the cloud function.)</p>
  
  <p>It's a bit of a treasure hunt to figure out how to do such a simple thing.</p>
  
  <ol>
  <li><p>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."</p>
  
  <p><a href="https://firebase.google.com/docs/functions/firestore-events" rel="noreferrer">https://firebase.google.com/docs/functions/firestore-events</a></p></li>
  <li><p>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.</p>
  
  <p><code>var defaultFirestore = admin.firestore();</code></p>
  
  <p>"The default Firestore client if no app is provided or the Firestore client associated with the provided app."</p>
  
  <p><a href="https://firebase.google.com/docs/reference/admin/node/admin.firestore" rel="noreferrer">https://firebase.google.com/docs/reference/admin/node/admin.firestore</a></p></li>
  <li><p>That link resolves to a general overview page with no direct clue on figuring out the next thing.</p>
  
  <p><a href="https://cloud.google.com/nodejs/docs/reference/firestore/0.10.x/" rel="noreferrer">https://cloud.google.com/nodejs/docs/reference/firestore/0.10.x/</a></p></li>
  <li><p>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.</p>
  
  <p><a href="https://cloud.google.com/nodejs/docs/reference/firestore/0.10.x/FirestoreClient#getDocument" rel="noreferrer">https://cloud.google.com/nodejs/docs/reference/firestore/0.10.x/FirestoreClient#getDocument</a></p>
  
  <p><code>var formattedName = client.anyPathPath("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
  client.getDocument({name: formattedName}).then(function(responses) {
    var response = responses[0];
    // doThingsWith(response)
  })</code></p></li>
  </ol>
  
  <p>So, I'm trying to combine all of this information into a Google cloud function that will read from another document.</p>
  
  <pre><code>const functions = require('firebase-functions');
  
  const admin = require('firebase-admin');
  admin.initializeApp(functions.config().firebase);
  
  exports.updateLikeCount4 = functions.firestore
      .document('likes/{likeId}').onWrite((event) =&gt; {
          return admin.firestore()
              .getDocument('ruleSets/1234')
              .then(function(responses) {
                   var response = responses[0];
                   console.log('Here is the other document: ' + response);
               })
      });
  </code></pre>
  
  <p>That approach fails with:</p>
  
  <pre><code>admin.firestore.getDocument is not a function
  </code></pre>
  
  <p>I've also tried. admin.firestore.document, admin.firestore.doc, admin.firestore.collection, and many more. None of them seem to be a function.</p>
  
  <p>All I want is to read from another Firestore document in my Google cloud function.</p>
  
  <p>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!</p>