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

前端 未结 3 428
情话喂你
情话喂你 2020-12-17 19:58

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

相关标签:
3条回答
  • 2020-12-17 20:23

    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.

    0 讨论(0)
  • 2020-12-17 20:30

    When you add a document to Cloud Firestore, the server can affect the data that is stored. A few ways this may happen:

    • If your data contains a marker for a server-side timestamp, the server will expand that marker into the actual timestamp.
    • Your data data is not permitted according to your server-side security rules, the server will reject the write operation.

    Since the server affects the contents of the Document, the client can't simply return the data that it already has as the new document. If you just want to show the data that you sent to the server in your client, you can of course do so by simply reusing the object you passed into setData(...)/addDocument(data: ...).

    0 讨论(0)
  • 2020-12-17 20:34

    This appears to be an arbitrary limitation of the the Firestore Javascript API. The Firestore REST API returns the updated document on the same call.

    https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents/patch

    0 讨论(0)
提交回复
热议问题