以下片段
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/