title: How to call firestore getDocument within a cloud function and send it's response back to the client using cloud function?
body: <p>I am new to javascript/nodeJS. I'm trying to implement a http firebase cloud function which receives userID in it's body. Once it is received I need to call the firestore getDocument function and get it's corresponding data, manipulate with these data and send back to the client in the http cloud function response. </p>
  
  <p>My code is as follows, I'm getting an empty response in the Postman. However I could notice in my server logs, following statement within my firestore getDocument prints the correct data. </p>
  
  <pre><code>console.log('Document data:', user);     
  </code></pre>
  
  <p>However when I print the same data in my http cloud function it is empty.</p>
  
  <pre><code>console.log('User data:', user);
  </code></pre>
  
  <p>According to my understanding even though firestore gets the actual data, http cloud function send it's response before retrieving these actual data which result in an empty response.</p>
  
  <p>Appreciate your insight. Thanks in advance!</p>
  
  <pre><code>const functions = require('firebase-functions');
  const admin = require('firebase-admin');
  
  admin.initializeApp(functions.config().firebase);
  let db = admin.firestore();
  
  var getRequestedUserDetails = function (userId, db) {
    var user;
    var cityRef = db.collection('users').doc(userId);
    var getDoc = cityRef.get()
      .then(doc =&gt; {
        if (!doc.exists) {
          console.log('No such document!');
          return user;
        } else {
          user = doc.data();
          console.log('Document data:', user);
          return user;
        }
      })
      .catch(err =&gt; {
        console.log('Error getting document', err);
        throw new Error('Error getting document', err);
      });
  
      return user;
  }
  
  exports.helloWorld = functions.https.onRequest((request, response) =&gt; {
  
    if (request.method !== "POST") {
      response.status(400).send('Invalid HTTP Request');
      return;
    }
  
    userId = request.body.userId;
  
    var user = getRequestedUserDetails(userId, db);
    console.log('User data:', user);
  
    response.send(user);
  
  });
  </code></pre>