Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get firebase firestore generated id

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

like image 229
Jama Mohamed Avatar asked Mar 24 '18 08:03

Jama Mohamed


People also ask

How to get document by id in Firebase 9 Cloud Firestore?

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.

How do I get data from Cloud Firestore?

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.

How to get a specific document from a specific collection in FireStore?

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.


1 Answers

Try this

firetoreDB.collection("collection").doc().id
like image 187
Jama Mohamed Avatar answered Oct 19 '22 03:10

Jama Mohamed