i had an issue where i would like to generate the unique id without sending any data. Below is my code of getting the id, but i would like to get it before i could send the actual data to the database
var getUniqueId = firetoreDB.collection("collction").add({
data: "data"
}).then(function (doc) {
console.log(doc.id);
}).catch(function (error) {
console.log(error);
});
The above code gets me the id but i wanted to get it without out sending a test data to the database
Learn how to get document by ID using the getDoc () method in Firebase version 9 Cloud Firestore Database. The sample Firestore Database has a cities collection that has four documents in it like in the screenshot below. Let’s get the first document of the cities collection by id.
Get data with Cloud Firestore. There are two ways to retrieve data stored in Cloud Firestore. Either of these methods can be used with documents, collections of documents, or the results of queries: Call a method to get the data. Set a listener to receive data-change events.
Import Firestore Database and de-structure the three methods that we need: doc () → It takes references of database, collection name and ID of a document as arguments getDoc () → getDoc () query gets data of a specific document from collection based references mentioned in the doc () method.
Try this
firetoreDB.collection("collection").doc().id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With