0

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 ??

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121

1 Answers1

0

You should use the set() method with the merge option, as follows:

let docRef = database.collection("PDFPublic").doc("January").collection("10").doc("Info");
let doc = await docRef.get();
await docRef.set(
     { Links: firebase.firestore.FieldValue.arrayUnion({ image: pathtostorage, pdf: pathtostorage }) },
     { merge: true }
).catch((e) => console.log(e));

As explained in the doc, this will have the following effect: If the document does not exist, it will be created. If the document does exist, the data will be merged into the existing document without overwriting the entire document.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • @RenuadTarnec The given answer does not help much i guess in the situation that i need to handle. Let me try to explain it better (i hope), 10 users fires the function to use it . Function executes it checks if there is document of current date exists. if not it tries to create the document else if found then it will simply executes the update operation. The Case is that if every executed functions tries to create the document and uses the `set` function then the data will be overwritten with the new one as using `set` won't update but creates the data. I don't want to let this happen. – ChandraShekharAazad Jan 07 '21 at 17:13
  • @ChandraShekharAazad What you mention in your comment about the `set()` method is not totally correct: If you use the `set()` method **without** the merge option, it indeed works as you described: anytime you call it it create a new document or override the existing one. However, when used **with the merge option** the behaviour of the `set()` method is different: If the document does not exist, it will create it (exactly like the `set()` method **without merge option** does). – Renaud Tarnec Jan 07 '21 at 20:51
  • **But**, if the document already exists, the `set()` method **with the merge option** will **NOT** overwrite it but it will act exactly like the `update()` method, updating the fields passed to the method but letting the other fields untouched. So, again, I think this is the solution to your problem. More info on the merge option in the [doc](https://googleapis.dev/nodejs/firestore/latest/global.html#SetOptions). – Renaud Tarnec Jan 07 '21 at 20:52
  • Also, look at the following answer: https://stackoverflow.com/a/46600599/3371862 – Renaud Tarnec Jan 07 '21 at 20:55
  • @ChandraShekharAazad Hello, did you have the opportunity to look at the additional comments/explanations? – Renaud Tarnec Jan 09 '21 at 10:43
  • I Know about the field value and the merge options but my concern was about the concurrent firing of the function. But have to see if that gonna come up or not. For now Thank you very much for Your time and i've accepted your answer. See yaa!! Good Day !! – ChandraShekharAazad Jan 10 '21 at 09:28