Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore REST API add Timestamp

I am trying to add Data to Cloud Firestore via the REST API (https://developers.google.com/apis-explorer/#search/firestore/firestore/v1beta1/firestore.projects.databases.documents.createDocument)

I am able to create a new Document with e.g. an Integer Value, so the connection and the syntax seems to be ok.

In the next step I want to add the server side timestamp to the document. Because I make the POST-Request from an ESP32 and the time is not available. The Request-Body looks like:

{  
   "fields":{  
      "myTime":{  
         "timestampValue":"SERVER_TIME_STAMP"
      }
   }
}

What do I have to write for SERVER_TIME_STAMP? For other languages there seems to be an Constant like firebase.database.ServerValue.TIMESTAMP that the server will replace with its current time. But the API does not accept values like thees.

The Error-msg is

"code": 400,
  "message": "Invalid value at 'document.fields[0].value.timestamp_value' (type.googleapis.com/google.protobuf.Timestamp), Field 'timestampValue', Illegal timestamp format; timestamps must end with 'Z' or have a valid timezone offset.",
like image 428
Lukas Backhaus Avatar asked Dec 27 '18 10:12

Lukas Backhaus


People also ask

How do you post a timestamp on firestore?

firestore. FieldValue. serverTimestamp() method. The actual timestamp will be computed when the doc is written to the Firestore.

How do I get the date from firestore timestamp?

To convert a Firestore Timestamp into a Javascript date, just call . toDate() on the Timestamp.

What is the format of firestore timestamp?

Date and time in the Firestore are saved in their proprietary date format — called Timestamp. Firestore timestamp is just a simple object with two properties — seconds and nanoseconds, which apparently has some advantages over “classic” date format.


1 Answers

For REST API, you should use DocumentTransform -> FieldTransform -> setToServerValue -> set ServerValue as REQUEST_TIME.

Seems like DocumentTransform is only available for use on write and commit API and not createDocument or patch. You can use commit API as a substitute of patch.

https://firebase.google.com/docs/firestore/reference/rest/v1beta1/Write#ServerValue

like image 142
5argon Avatar answered Oct 05 '22 23:10

5argon