Cloud Firestore REST API - Add server timestamp

不想你离开。 提交于 2020-05-24 02:52:25

问题


I'm using an Arduino with an ESP8266-01 module to upload a value to a Cloud Firestore database using the createDocument API with the following payload:

{
    "fields": {
        "distance": {
            "integerValue": "555"
        }
    }
}

I do a POST-request to a route like this:

https://firestore.googleapis.com/v1beta1/projects/<MY_PROJECT>/databases/(default)/documents/<SOME_COLLECTION>?key=MY_VERY_SECRET_KEY

That all works, but I would like to add the server timestamp as well. I've found a few answers here on stackoverflow, but I have not been able to make any of them work.

How can I add the server timestamp to the created document? What I want is for the following to be created:

{
    "fields": {
        "distance": {
            "integerValue": "555"
        },
        "timestamp" : {
            "DATETIME": SERVER_TIMESTAMP
        }
    }
}

Any help appreciated.


回答1:


What I ended up doing in the end was the following:

A POST request to a route like this:

https://firestore.googleapis.com/v1beta1/projects/<MY_PROJECT>/databases/(default)/documents:commit?&key=<MY_VERY_SECRET_KEY>

With the following payload:

{
    "writes": [
        {
            "update": {
                "name": "projects/<MY_PROJECT>/databases/(default)/documents/<COLLECTION_ID>/<DOCUMENT_ID>",
                "fields": {
                    "distance": {
                        "integerValue": "555"
                    }
                }
            }
        },
        {
            "transform": {
                "document": "projects/<MY_PROJECT>/databases/(default)/documents/<COLLECTION_ID>/<DOCUMENT_ID>",
                "fieldTransforms": [
                    {
                        "fieldPath": "servertime",
                        "setToServerValue": "REQUEST_TIME"
                    }
                ]
            }
        }
    ]
}

Where I generate a new DOCUMENT_ID (e.g. a GUID) instead of having cloud firestore generate one for me.



来源:https://stackoverflow.com/questions/57779415/cloud-firestore-rest-api-add-server-timestamp

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