Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore: get document back after adding it / updating it without additional network calls

Is it possible to get document back after adding it / updating it without additional network calls with Firestore, similar to MongoDB?

I find it stupid to first make a call to add / update a document and then make an additional call to get it.

like image 586
Raj Chaudhary Avatar asked Oct 06 '18 10:10

Raj Chaudhary


People also ask

What is onSnapshot in firebase?

You can listen to a document with the onSnapshot() method. An initial call using the callback you provide creates a document snapshot immediately with the current contents of the single document. Then, each time the contents change, another call updates the document snapshot.

How do I update firestore documents?

Firestore Update Entire DocumentgetDatabase() → where we want to update a document. doc() → where we'll be passing references of database, collection and ID of a document that we want to update. setDoc() → where we actually pass new data that we want to replace along with the doc() method.

How do you reset firestore?

There is currently no way to wipe out a Firestore database, other then to delete all the document using the normal ways that you would do so. Delete them in the console, or delete them by querying for them, iterating the results, and deleting each document.

Does firestore Create collection if not exists?

Collections and documents are created implicitly in Cloud Firestore. Simply assign data to a document within a collection. If either the collection or document does not exist, Cloud Firestore creates it.

How do I update an existing document in Cloud Firestore?

All you have to do is add the exact document field name, found in the Firestore Database inside the data object, with a new value ON. There are two scenarios in which the updateDoc () query does not affect any existing document in Cloud Firestore if you want to update:

How do I retrieve data stored in 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.

What happens when I set a listener in Cloud Firestore?

When you set a listener, Cloud Firestore sends your listener an initial snapshot of the data, and then another snapshot each time the document changes. Note: While the code samples cover multiple languages, the text explaining the samples refers to the Web method names.

What is the difference between FireStore new client and close client?

client, err := firestore. NewClient ( ctx, projectID) return fmt. Errorf ( "firestore.NewClient: %v", err) defer client. Close () it := client. Collection ( collection ).


1 Answers

As you have probably seen in the documentation of the Node.js (and Javascript) SDKs, this is not possible, neither with the methods of a DocumentReference nor with the one of a CollectionReference.

More precisely, the set() and update() methods of a DocumentReference both return a Promise containing void, while the CollectionReference's add() method returns a Promise containing a DocumentReference.


Side Note (in line with answer from darrinm below): It is interesting to note that with the Firestore REST API, when you create a document, you get back (i.e. through the API endpoint response) a Document object.

like image 129
Renaud Tarnec Avatar answered Sep 28 '22 14:09

Renaud Tarnec