Skip to content
Advertisement

How do I add multiple documents in firebase 9 with javascript?

Their documentation only shows how to set multiple documents with batch, but I need to add new docs and doing it one by one with a loop seems like a bad solution

JavaScript

Advertisement

Answer

You can add a document using Batched Writes by creating a DocumentReference first as shown below:

JavaScript

If you don’t want to use Batched Writes and use a loop (which you might have to if you want to add more than 500 documents at once). Try the following:

The map() does not return a Promise but addDoc() does. So you must await the addDoc() instead. You cannot use async-await with a forEach loop so you can either use for-of loop or use Promise.all() as shown below:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement