0

I Wanted To Ask If It Is Possible To Make A New Document With A UID If It DOESN'T Exist But if it exists NOT To Do Anything (and if possible return an error) In Firestore. (In Modular Javascript)

And If It Is Possible How?
Note: I Already Read This Question:StackOverFlow 46888701 But It Doesn't Fit My Requirements because after creating the document I want to be able to update it too.
Edit: I Wanted To Know Without Using getDoc because when i use it acts like a read and i don't want to spend lots of my no of reads from my limit.

mrtechtroid
  • 658
  • 3
  • 14

2 Answers2

2

You should first try to get the document and check if it exists then proceed to your document set/update. See sample code below:

import { doc, getDoc } from "firebase/firestore";

const docRef = doc(db, "<collection>", "<UID>");
const docSnap = await getDoc(docRef);

if (docSnap.exists()) {
  console.log("Document exist!");
  // Throws an error.
  throw new Error('Document Exist!');
} else {
  await setDoc(docRef, {
    // Document Data
  });
}

For more relevant information, check out these documentations:


Edit:

If you don' t want to use getDoc then you have the option to use updateDoc, it will produce an error but you can still execute a setDoc method on the catch method. On this approach, you're doing a fail-safe practice that you're responding in the event of failure. See code below:

const docRef = doc(db, "<collection>", "<UID>");

  // Produces error log if no document to update
  updateDoc(docRef, {
    // document data
  })
  .catch((error) => {
    // console.log(error);
    setDoc(docRef, {
      // document data
    });
  });

According to the documentation, an update is just a write operation:

Charges for writes and deletes are straightforward. For writes, each set or update operation counts a single write.

We have established that an update is just a write operation (there's no reading involved). A write is a change in a document, since you're not changing anything because the document didn't exist then you won't be charged at all.

Marc Anthony B
  • 3,635
  • 2
  • 4
  • 19
  • I Wanted To Know Without Using getDoc because when i use it acts like a read and i don't want to spend lots of my no of reads from my limit. – mrtechtroid Apr 07 '22 at 07:59
  • @mrtechtroid , you could use `updateDoc`, it will log an error if the document does not exist but you can still continue setting a document. I've updated my answer. – Marc Anthony B Apr 07 '22 at 09:20
1

In web version 9, the function that can help you create a document is named setDoc(), which creates or overwrites a document at a specific document reference.

How to create a document if the document doesn't exist or else don't do anything?

If you want to achieve that, you have to check if the document already exists. If it doesn't exist, create it using setDoc(), otherwise, take no action, but do not use the updateDoc() function in this case.

Remember that the updateDoc() function helps only when you want to update some fields of a document without overwriting the entire document. If the document doesn't exist, the update operation will fail.

Edit:

According to your edited question, please note that there is no way you can know if a document exists, without checking it explicitly. You can indeed not do that check, but you'll end up overwriting the document over and over again. Please also note, that a write operation is more expensive than a read operation. So that's the best option that you have.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Also One More Question When I Use the `setDoc()` without specifying the document name Firebase automatically generates a ID For The Document, I know that, but does it check that ID is not used by any other document? – mrtechtroid Apr 07 '22 at 08:10
  • 1
    No, it doesn't because it isn't required. Why? Because [all those IDs that are generated are completely are unique](https://stackoverflow.com/questions/53119133/firestore-generated-unique-ids-for-more-then-1-collection). – Alex Mamo Apr 07 '22 at 08:15
  • Can I help you with other information? – Alex Mamo Apr 08 '22 at 05:07
  • So you say that my answer helped you solve the issue? – Alex Mamo Apr 08 '22 at 06:50
  • The logic for charges in Firestore is really simple. If for example, a document is written on the Firebase servers, you are indeed charged with a document write. If a document is read from the Firebase server, you are indeed charged with a document read. Without knowing why one of those operations fails, it's hard to say anything about it. It's also worth mentioning that there are a lot of different ways that an operation can fail, and they might not all consider billing at all. – Alex Mamo Apr 08 '22 at 07:03