title: Pub/Sub Cloud Function does not Update Document in Subcollection
body: <p>I am trying to update a field in my document in Firestore. The general location of the document would be &quot;/games/{userId}/userGames/{gameId}. And in this game, there is a property called &quot;status&quot; which changes accordingly to the games start and end time.</p>
  <p>As you can guess, the if the start time is bigger than the &quot;now&quot; timestamp and the status is &quot;<em>TO_BE_PLAYED</em>&quot;, the game will begin and the status will be 1, &quot;<em>BEING_PLAYED</em>&quot;. Also, if the end time is bigger than the &quot;now&quot; timestamp and the status is &quot;<em>BEING_PLAYED</em>&quot;, the game will end, therefore the status will be 2, &quot;<em>PLAYED</em>&quot;. I want to create a cloud function that is capable to do so.</p>
  <p>However, even if the function logs output 'ok', the values are never updated. Unfortunately, I do not have that much experience in Javascript too.</p>
  <p><strong>THE CODE</strong></p>
  <pre><code>const functions = require('firebase-functions');
  
  const admin = require('firebase-admin');
  admin.initializeApp();
  
  const STATUS_PLAYED = 2;
  const STATUS_BEING_PLAYED = 1;
  const STATUS_TO_BE_PLAYED = 0;
  
  exports.handleBeingPlayedGames = functions.runWith({memory: &quot;2GB&quot;}).pubsub.schedule('* * * * *')
       .timeZone('Europe/Istanbul') // Users can choose timezone - default is America/Los_Angeles
       .onRun(async () =&gt; {
         // current time &amp; stable
                              // was Timestamp.now();
         const now = admin.firestore.Timestamp.fromDate( new Date());
  
         const querySnapshot = await db.collection(&quot;games&quot;).get();
         const promises = [];
  
         querySnapshot.forEach( doc =&gt; {
             const docRef = doc.ref;
             console.log(docRef);
             promises.push(docRef.collection(&quot;userGames&quot;).where(&quot;status&quot;, &quot;==&quot;, STATUS_BEING_PLAYED).where(&quot;endtime&quot;, &quot;&lt;&quot;, now).get());
         });
         const snapshotArrays = await Promise.all(promises);
  
         const promises1 = [];
  
         snapshotArrays.forEach( snapArray =&gt; {
             snapArray.forEach(snap =&gt; {
                 promises1.push(snap.ref.update({
                     &quot;status&quot;: STATUS_PLAYED,
                 }));
             });
         });
  
         return Promise.all(promises1);
  
     });
  
  exports.handleToBePlayedGames = functions.runWith({memory: &quot;2GB&quot;}).pubsub.schedule('* * * * *')
       .onRun(async () =&gt; {
         // current time &amp; stable
  
                              // was Timestamp.now();
         const now = admin.firestore.Timestamp.fromDate(new Date());
  
         const querySnapshot = await db.collection(&quot;games&quot;).get();
         const promises = [];
  
         querySnapshot.forEach( async doc =&gt; {
             const docData = await doc.ref.collection(&quot;userGames&quot;).where(&quot;status&quot;, &quot;==&quot;, STATUS_TO_BE_PLAYED).where(&quot;startTime&quot;, &quot;&gt;&quot;, now).get();
             promises.push(docData);
         });
         const snapshotArrays = await Promise.all(promises);
  
         const promises1 = [];
  
         snapshotArrays.forEach( snapArray =&gt; {
             snapArray.forEach(snap =&gt; {
                 promises1.push(snap.ref.update({
                     &quot;status&quot;: STATUS_BEING_PLAYED,
                 }));
             });
         });
  
         return Promise.all(promises1);
  
     });
  
  </code></pre>