mozpider
mozpider

Reputation: 364

Firestore - only able to create two collections at a time

I am uploading an initial data to firebase-firestore. The data is to be used as a fixture to develop my frontend further. I do not have errors, at least haven't caught one yet. The following two functions, are being used to upload data: Edited to explain better

async function uploadData() {
  const userId = 'AKFROjo1isTPRfjYYDsSehwZdIi1';

  const itemsListRef = await db.collection('users').doc(userId).collection('userData').doc('itemsList');
  await itemsListRef.set({ created: firebase.firestore.FieldValue.serverTimestamp() });

   await summaryList.forEach(async summary => {
    await itemsListRef.collection('summaryList').add(summary);
   });

   await coreCompetencyList.forEach(async coreCompetency => {
     await itemsListRef.collection('coreCompetencyList').add(coreCompetency);
   });

}

and a second function as -

async function uploadData2() {
  const userId = 'AKFROjo1isTPRfjYYDsSehwZdIi1';

  const itemsListRef = await db.collection('users').doc(userId).collection('userData').doc('itemsList');
  await itemsListRef.set({ created: firebase.firestore.FieldValue.serverTimestamp() });

  await educationList.forEach(async education => {
    await itemsListRef.collection('educationList').add(education);
  });

  await categoryList.forEach(async category => {
    await itemsListRef.collection('categoryList').add(category);
  });
}

It is being called as :

async function main() {
  try {
    await uploadData();
    await app.delete();
  } catch (e) {
    console.log('Data upload failed, reason:', e, '\n\n');
  }
}
main().then(r => console.log('Done.'));

I am surprised that I cannot put all 4 calls in a single function

Upvotes: 0

Views: 51

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

It's not a bug. This is the way it was intended to work.

In actuality, collections are not really entities that require creation or deletion like folders in a filesystem. They are just virtual containers for documents that spring into existence immediately when the first document in created under it, and automatically disappear when the last document in deleted. You can think of them more like units of organization that make indexing and security rules possible to implement.

Upvotes: 1

Related Questions