title: Batch write to firebase cloud firestore
body: <p>I want to create a new collection and add thousands of documents sized ~ 1-2K to it. I already have data in json so I thought this would be easy.</p>
  
  <p>I understand that batch can have 500 writes at a time so to break it into chunks of 500 I wrote the following code. Though for testing purpose I am running it with chunks of 20 and my test json has 72 objects.</p>
  
  <p>But I keep getting the following error</p>
  
  <pre><code>node_modules\@google-cloud\firestore\src\write-batch.js:148
    throw new Error('Cannot modify a WriteBatch that has been committed.');
    ^
  
  Error: Cannot modify a WriteBatch that has been committed.
  </code></pre>
  
  <p>My code is as follows</p>
  
  <pre><code>var dataObj = JSON.parse(fs.readFileSync('./bigt.json'))
  var tmpdd = dataObj.slice(0, 72)
  var batch = db.batch();
  
  console.log(tmpdd.length)
  
  let tc = tmpdd.length
  let lc = 0
  let upperLimit = 20, dd = null
  
  while(lc&lt;=tc){
  
      dd = tmpdd.slice(lc, upperLimit )
  
      console.log(lc, upperLimit)
      dd.map(
      o =&gt; batch.set(db.collection('nseStocks').doc(o.Date+o.variable), o)
      )
  
      batch.commit().then(function () {
          console.log('Written to firestore', lc, lc + upperLimit)
      })
      .catch(
          (err) =&gt; console.log('Fail', err)
      )
  
      lc = upperLimit
      upperLimit = upperLimit + 20 
  
  }
  </code></pre>
  
  <p>Also it's weird that batch doesn't seem to be committed in every iteration of the loop. Ideally I would let Firestore determine document ids but apparently batch does not have add function.</p>
  
  <p>I have tried adding documents in a loop instead of doing batch writes. But it gives me timeout error after adding a few documents. And of course it's not practical for large number of documents.</p>
  
  <p>You could tell I am very new to Firestore and it's my second day playing with it.</p>
  
  <p>Please let me know if there are any obvious mistakes or better ways of achieving this seemingly simple task.</p>
  
  <p>Thanks</p>