Firestore时间戳记另存为地图

时间:2019-09-12 00:05:58

标签: javascript node.js firebase google-cloud-firestore

以下代码段

library(tidyverse)

# Test data, it does not matter.
data <- tibble(
  color = c(rep('color-a', 5), rep('color-b', 5), rep('color-c', 5), rep('color-d', 5)),
  x = rep(seq(0, 4, 1), 4),
  y = c(seq(0, .4, 0.1), seq(0, .4, 0.1) + 0.1, seq(0, .4, 0.1) + 0.3, seq(0, .4, 0.1) + 0.4)
)

# Plot
ggplot(data, aes(x = x, y = y, color = color)) +
  scale_color_discrete(guide="legend") +
  geom_point() +
  theme_minimal() +
  theme(legend.position = "bottom")

将此锦标赛对象传递给可调用的云函数(其唯一目的是保存作为文档传递的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) } )会将tournament字段另存为具有属性start和{ {1}},而不是Firestore中的时间戳。

我也尝试只做seconds,但这也无法带来将时间戳记保存在Firestore中的预期结果。

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

miliseconds

(tournamentData是从前端传递的对象)

1 个答案:

答案 0 :(得分:1)

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

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

相关问题