Firestore REST API 添加时间戳 [英] Firestore REST API add Timestamp

查看:31
本文介绍了Firestore REST API 添加时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 REST API (https://developers.google.com/apis-explorer/#search/firestore/firestore/v1beta1/firestore.projects.databases.documents.createDocument)>

我可以创建一个新文档,例如一个整数值,所以连接和语法似乎没问题.

在下一步中,我想将服务器端时间戳添加到文档中.因为我从 ESP32 发出 POST-Request 并且时间不可用.请求体看起来像:

<代码>{领域":{我的时间":{"timestampValue":"SERVER_TIME_STAMP"}}}

我必须为 SERVER_TIME_STAMP 写什么?对于其他语言,似乎有一个像 firebase.database.ServerValue.TIMESTAMP 这样的常量,服务器将用其当前时间替换.但是 API 不接受像 thees 这样的值.

错误信息是

"code": 400,"message": "无效值在 'document.fields[0].value.timestamp_value' (type.googleapis.com/google.protobuf.Timestamp),字段 'timestampValue',非法时间戳格式;时间戳必须以 'Z' 结尾或具有有效的时区偏移量.",

解决方案

对于 REST API,你应该使用 DocumentTransform -> FieldTransform -> setToServerValue -> 将 ServerValue 设置为 REQUEST_TIME.

似乎 DocumentTransform 只能用于 writecommit API 而不是 createDocument>补丁.您可以使用 commit API 代替 patch.

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

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.",

解决方案

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

这篇关于Firestore REST API 添加时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆