gpt4 book ai didi

javascript - 从数据库获取文档快照并将其 ServerValue.TIMESTAMP 值转换为日期

转载 作者:行者123 更新时间:2023-12-02 23:56:17 24 4
gpt4 key购买 nike

当我们从数据库读取 DocumentSnapshot 时,所有 ServerValue.TIMESTAMP 字段的值为 { '.sv': 'timestamp' }.我们如何将该值读取为 Date 或等效的时间相关值?

这是我们尝试过的:

// create a document reference
const documentReference = firestoreDb.collection('test').doc();

// write the document to the database
const writeResult = await documentReference.set({
timestamp: firebase.database.ServerValue.TIMESTAMP
});

// read the document from the database
const documentSnapshot = await documentReference.get();

// the timestamp value is { '.sv': 'timestamp' }
console.log(documentSnapshot.data().timestamp);

最佳答案

您正在尝试使用 Realtime Database server timestamp 。这与 Firestore server timestamp 不同。改用这个:

Node SDK

import { FieldValue } from '@google-cloud/firestore';

const timetamp = FieldValue.serverTimestamp();

网络 SDK

const timetamp = firebase.firestore.FieldValue.serverTimestamp();

关于javascript - 从数据库获取文档快照并将其 ServerValue.TIMESTAMP 值转换为日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55362550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com