Firestore 时间戳保存为地图

Bad*_*dgy 4 javascript node.js firebase google-cloud-firestore

以下片段

const start = new Date(this.date + 'T' + this.time);
console.log(start); // Thu Sep 12 2019 04:00:00 GMT+0200

const tournament:Tournament = {
      start: firebase.firestore.Timestamp.fromDate(start)
}
Run Code Online (Sandbox Code Playgroud)

将此锦标赛对象传递给可调用云函数(其唯一目的是将tournament传递的数据保存为文档)会将start字段保存为具有属性的地图seconds,而miliseconds不是 Firestore 中的时间戳。

我也尝试这样做start: start,但这也没有带来在 Firestore 中保存时间戳的预期结果。

仅供参考,这是功能代码精简后的样子:

firestore.collection('tournaments').doc(slugUrl).set(tournamentData)

(tournamentData是前端传入的Object)

Dou*_*son 6

您的 Timestamp 对象必须先序列化为 JSON,然后才能发送到函数。默认序列化将时间戳分解为其自然秒和纳秒值结果对象。所有类型信息都会丢失。

在您的函数中,您必须从传递给函数的数据中读取这些单独的值,使用其双参数构造函数将它们转换回正确的 Timestamp 对象,然后将该对象写入 Cloud Firestore。然后才会保存为时间戳类型字段。