I am working on an app the will inject docs in to firestore database those docs will have an id generated by uuid v4 the question is could 2 user every user on a different browser created same doc with the same id ? since the uuid function is the same ? if a duplicated could occur it will overwrite someone's data! in case if uuid is generation the ids not by timestamp. other wise 2 users will have same reference which also really bad case ! could npm uuid be used for this purpose ?
import { v4 as uuidv4 } from 'uuid';
const id = uuidv4();
If you want to get the ID of a document you're adding to Firestore before the document is actually added, have a look at this section of the Firebase documentation on adding a document:
In some cases, it can be useful to create a document reference with an auto-generated ID, then use the reference later. For this use case, you can call
doc()
:import { collection, doc, setDoc } from "firebase/firestore"; // Add a new document with a generated id const newCityRef = doc(collection(db, "cities")); // later... await setDoc(newCityRef, data);
This is the JavaScript code for v9 or later of the SDK. Code for other languages can be found at the link above.