Renauds answer is good and complete but just to simplify everything a bit, the following will work but will possibly timeout depending on the value of the counter.
const functions = require('firebase-functions');
exports.addDummyUsers = functions.https.onRequest(async (request,response)=>{
let dbb=admin.firestore();
let counter = request.query.counter;
let userName = ""
for(let i=0;i<counter;i++){
userName = "Lone" + i.toString().padStart(4, '0') // pad to length 4 with leading zeros
await dbb.collection('Users').doc(userName).set({
email: 'Dummy',
name: 'Dummy',
phoneNumber: 'Dummy'
});
console.log("Dummy " + counter + ' added');
}
response.send(counter + ' Dummy Values Created!');
});
The problems in your code are as follows:
In the line dbb.collection('Users').doc('Lone').set({
you do not add i to to doc name so you keep overwriting the same doc "Lone". Renaud fixed this.
Secondly there is no await, so the cloud function will finish before the documents are added so some of the documents will not be added. The async in the
exports.addDummyUsers = functions.https.onRequest(async (request,response)=>{
then means you can use await and this means that the code will wait until each document has been added. this is much more efficient if you do batch writes but as Renaud pointed out, 500 writes (or the combination of adds, updates or deletes) is the limit.
You could also do the same by doing the batch writes inside a loop if counter is greater than 500.
Also, to make the sorting look more logical in the firebase console, I have padded the counter value to 4 (obviously this would not be enough if the counter was passed as 10000)
To change the timeout do the following
This is by default set to 60 seconds on cloud functions although this can be changed by using runWith and the timeout and memory options if you need to increase runtime memory.
const runtimeOpts = {
timeoutSeconds: 300,
memory: '1GB'
}
exports.addFirestoreData = functions.wunWith(runtimeOpts).https.onCall(async data => {
One other note. You need to be careful that you do not create hotspots in your data by not using automatically generated document ids.
Your users are going to be all in the same area as the names are all Lone0001,Lone002 etc. which will mean that accesses will clash a lot and therefore reduce performance.
The point of the automatically generated document Ids is that they are designed to avoid hotspots. This discusses best practices https://cloud.google.com/firestore/docs/best-practices