title: Unpredictable / inconsistent outcomes in writing and getting counter data to Firestore database
body: <p>Currently I'm creating a Google Action game. I want to give users extra points when they have a higher loginstreak. So if a user logs in 2 times, and he gives the correct answer, he should receive 2 points instead of 1. 3 logins = 3 points etc.</p>
  
  <p>For this, I have a weeklysuccescount (# of correct answers in a week) and a loginstreakcount. When I test my app, it provides inconsistent outputs. So instead of granting me 2 points at my second login, it only provides me 1. During another test doesn't grant any points at all and my total points stay the same as before.</p>
  
  <p>When I log 'loginstreakcount', the value differs from the value in the database. In fact, most of the time it shows 1 login less. </p>
  
  <p>I searched online for this problem and I found that it might have to do with the limitation of Firestore 1 QPS per document. However, I don't know if this is the real problem. </p>
  
  <pre><code>const userRef = db.collection('users').doc(payload.email)     
  userRef.update({loginstreak: admin.firestore.FieldValue.increment(1)})                                         
  const getuser = userRef.get()
  return getuser
  .then(doc =&gt; {
      if (!doc.exists) {
          console.log('No such document!');
        } else {
  
  
  
  
          if (doc.data().loginstreak === 2) {
            userRef.update({weeklysuccescount: admin.firestore.FieldValue.increment(1)})
            conv.add('Hooray')
            console.log("If statement 2")
            conv.close('bye')
          }
  
          else if (doc.data().loginstreak === 3) {
            userRef.update({weeklysuccescount: admin.firestore.FieldValue.increment(2)})
            conv.add('Hooray')
            console.log("If statement 3")
            conv.close('bye')
          }
  
          else if (doc.data().loginstreak === 4) {
            userRef.update({weeklysuccescount: admin.firestore.FieldValue.increment(3)})
            conv.add('Hooray')
            console.log("If statement 4")
            conv.close('bye')
          }
  
          else if (doc.data().loginstreak === 5) {
            userRef.update({weeklysuccescount: admin.firestore.FieldValue.increment(4)})
            conv.add('Hooray')
            console.log("If statement 5")
            conv.close('bye')
          }
  
          else if (doc.data().loginstreak === 6) {
            userRef.update({weeklysuccescount: admin.firestore.FieldValue.increment(5)})
            conv.add('Hooray')
            console.log("If statement 6")
            conv.close('bye')
          }
  
          else if (doc.data().loginstreak === 7) {
            userRef.update({weeklysuccescount: admin.firestore.FieldValue.increment(6)})
            conv.add('Hooray')
            console.log("If statement 7")
            conv.close('bye')
          }
  
          else {                              
            userRef.update({weeklysuccescount: admin.firestore.FieldValue.increment(1)})
            console.log('Loginstreak: ' + doc.data().loginstreak)
            conv.add('Hooray')
            conv.close('bye')
          }
        }})
  </code></pre>
  
  <p>UPDATE: </p>
  
  <p>I tried to implement the answer, but in the DB the score count is valid, however, in the Action it speaks a wrong score. So, the promise does not seem to work out yet. Here is my updated code:</p>
  
  <pre><code>const userRef = db.collection('users').doc(payload.email)     
  
                          return userRef.update({loginstreak: admin.firestore.FieldValue.increment(1)})
                          .then(() =&gt; {    
  
                          const getuser = userRef.get()
                          return getuser
                          .then(doc =&gt; {
                              if (!doc.exists) {
                                  console.log('No such document!');
                                } else {
  
  
                                  if (doc.data().loginstreak === 2) {
                                    return userRef.update({wekelijkssuccescount: admin.firestore.FieldValue.increment(1)})                                     
                                    .then(() =&gt; {
                                    conv.add('Hooray')
            console.log("If statement 2")
            conv.close('bye')
                                  })
                                  }
  
                                  else if (doc.data().loginstreak === 3) {
                                    return userRef.update({wekelijkssuccescount: admin.firestore.FieldValue.increment(2)}) 
                                    .then(() =&gt; {
                                    conv.add('Hooray')
            console.log("If statement 3")
            conv.close('bye')
                                  })
                                  }
  
                                  else if (doc.data().loginstreak === 4) {
                                    return userRef.update({wekelijkssuccescount: admin.firestore.FieldValue.increment(3)}) 
                                    .then(() =&gt; {
                                    conv.add('Hooray')
            console.log("If statement 4")
            conv.close('bye')
                                  })
                                  }
  
                                  else if (doc.data().loginstreak === 5) {
                                    return userRef.update({wekelijkssuccescount: admin.firestore.FieldValue.increment(4)}) 
                                    .then(() =&gt; {
                                    conv.add('Hooray')
            console.log("If statement 5")
            conv.close('bye')
                                  })
                                  }
  
                                  else if (doc.data().loginstreak === 6) {
                                    return userRef.update({wekelijkssuccescount: admin.firestore.FieldValue.increment(5)}) 
                                    .then(() =&gt; {
                                   conv.add('Hooray')
            console.log("If statement 6")
            conv.close('bye')
                                  })
                                  }
  
                                  else if (doc.data().loginstreak === 7) {
                                    return userRef.update({wekelijkssuccescount: admin.firestore.FieldValue.increment(6)}) 
                                    .then(() =&gt; {
                                    conv.add('Hooray')
            console.log("If statement 7")
            conv.close('bye')
                                  })
                                  }
  
                                  else {                              
                                    return userRef.update({wekelijkssuccescount: admin.firestore.FieldValue.increment(1)}) 
                                    .then(() =&gt; {
                                    console.log('Loginstreak: ' + doc.data().loginstreak)
                                    conv.add('Hooray')
  
            conv.close('bye')
                                  }) 
                                  } 
                                }}) })
  </code></pre>