In my website i offer user to crop pdfs and serve them as jpeg.
I save the pdf and jpeg to storage to take a look to see if the function executed as expected.
In my function i want to save the path to pdf and jpeg to firestore to see it on my custom dashboard.
The datastructer is like this:
Collection "PDFPublic" > Document "January" > Collection "07" > Document "Info" > PDF AND IMAGE PATH FIELDS
I save them in months documents (as category) and in subcollection of days and document named Info.
I check the existence of a document and create it like this:
let docRef = database.collection("PDFPublic").doc("January").collection("10").doc("Info");
let doc = await docRef.get();
if (doc.exists) {
await docRef.update({ Links: firebase.firestore.FieldValue.arrayUnion({ image: pathtostorage, pdf: pathtostorage }) }).catch((e) => console.log(e));
} else {
await docRef.set({ Links: [{ image: pathtostorage, pdf: pathtostorage }] }).catch((e) => console.log(e));
}
I create an array for pdfs converted on the same day.
Now the problem is if two or more users on a new day, say 10 January, executes the function at same time they will not find the document and will use the set
method which will recreate the field.
In that case the most data will be lost/overwrited.
How to handle this type of situation ??