title: Firebase Firestore update query doesn't execute because of the promise chaining. What am I doing wrong?
body: <p>This code is being executed in a firebase cloud function to make a stripe charge after being triggered by a document being added to firestore. This is just a snippet where the problem lies. The code is supposed to charge a card, and upon success, update the 'dexCoinBal' value in firestore with a newBalance. I approached this by getting the previous value and then moving along with the update. However, the update doesn't seem to run at all. It's probably something to do with chaining the promises but I can't figure out what it is exactly. If someone could point out what I'm doing wrong that'd be great. I clearly don't understand promises well enough.</p>
  
  <pre><code> function charge (tok, amt, curr, uid) {
          const token = tok;
          const amount = amt;
          const currency = curr;
          const fStor = admin.firestore();
  
          stripe.charges.create({
              amount,
              currency,
              description: 'Example charge',
              source: token
          }).then(function(result){
              return fStor.collection('users').get();
          }).then(function(doc){
              if(doc.exists){
                  const prev = doc.data().dexCoinBal;
                  var numDexCoins = amt / (0.25 * 100);
                  var newBal = numDexCoins + prev;
  
                  //Update the number of dexcoins in database
                  fStor.collection('users').doc(uid).update({
                      dexCoinBal: newBal
                  });
              }
          });   
      }
  </code></pre>
  
  <p>Full code below. Fixed the issue.</p>
  
  <pre><code>const functions = require('firebase-functions');
  const admin = require('firebase-admin');
  admin.initializeApp(functions.config().firebase);
  
  const fStor = admin.firestore();
  const settings = {timestampsInSnapshots: true};
  fStor.settings = settings;
  
  // TODO: Remember to set token using &gt;&gt; firebase functions:config:set stripe.token="SECRET_STRIPE_TOKEN_HERE"
  const stripe = require('stripe')(functions.config().stripe.token);
  
  function charge (tok, amt, curr, uid) {
      const token = tok;
      const amount = amt;
      const currency = curr;
  
      console.log("Outside");
      return stripe.charges.create({
          amount,
          currency,
          description: 'Example charge',
          source: token
      }).then(function(result){
          console.log("First Then");
          return fStor.collection('users').doc(uid).get();
      }).then(function(doc){
          console.log("Second Then");
          if(doc.exists){
              const prev = doc.data().dexCoinBal;
              var numDexCoins = amt / (0.25 * 100);
              var newBal = numDexCoins + prev;
  
              //Update the number of dexcoins in database
              fStor.collection('users').doc(uid).update({
                  dexCoinBal: newBal
              });
          }
      });   
  }
  
  exports.createCharge = functions.firestore
      .document('charges/{chargeId}')
      .onCreate((snap, context) =&gt; {
        // Get an object representing the document
        // e.g. {'name': 'Marie', 'age': 66}
        const data = snap.data();
  
        // access a particular field as you would any JS property
        const token = data.token;
        const amount = data.amount;
        const currency = data.currency;
        const uid = data.uid;
  
      // perform desired operations ...
        return charge(token, amount, currency, uid);
      });
  </code></pre>