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

久未见 提交于 2019-12-02 04:39:43

问题


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.


回答1:


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.




回答2:


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: ...).




回答3:


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



来源:https://stackoverflow.com/questions/52678252/firestore-get-document-back-after-adding-it-updating-it-without-additional-ne

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!