javascript - Firestore 时间戳保存为 map

标签 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)
}

将此锦标赛对象传递给可调用云函数,其唯一目的是保存作为文档传递的锦标赛,并将start字段保存为具有属性毫秒 而不是 Firestore 中的时间戳。

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

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

firestore.collection('锦标赛').doc(slugUrl).set(tournamentData)

(tournamentData是前端传入的Object)

最佳答案

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

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

关于javascript - Firestore 时间戳保存为 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57898146/

相关文章:

javascript - 使用 require 代替 import { a, b } from 'some-lib' ,等效的是什么?

java - ZipException:重复输入异常

android - 使用 Firebase 和 ChildEventListener 时如何在方向更改后保留 RecyclerView 的位置?

firebase - Flutter:调用 setData 时从 firstore 获取文档 ID

javascript - 如何在 Javascript 中将对象的字段名称转换为值

javascript - RequireJS 和 Angular 路由

node.js - 在 Express 应用程序的默认 connect.sid 浏览器 cookie 上设置域

node.js - 如何对每个请求执行 Nightmare ?

javascript - 当 Redux 连接状态改变时,有状态的 React-Native 功能组件不会重新渲染

javascript - 无法成组创建 svg 圆圈