'Firestore setDoc() method somehow generates a unique identifier when doc says it won't
Google documentation states that
When you use set() to create a document, you must specify an ID
However, I just learned that I CAN call setDoc without specifying the ID like this, and it will still generate a unique identifier.
await setDoc(doc(collctionRef), {
createdAt: new Date(),
updatedAt: new Date(),
});
Question
- Is the Firebase documentation outdated? or is there something I missed?
- If this is true,
addDocseems useless now. Or is there another use for it?
Solution 1:[1]
If you don't specify document ID in doc(collctionRef) then it'll generate one automatically. The same was in previous SDK:
// This would add a Document with random ID
// V8
colRef.doc().set({})
// V9
setDoc(doc(colRef))
From the same documentation,
Behind the scenes,
.add(...)and.doc().set(...)are completely equivalent, so you can use whichever is more convenient.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharmaraj |

