gpt4 book ai didi

node.js - Firebase Cloud Firestore 如何设置 snapShotOptions

转载 作者:行者123 更新时间:2023-12-03 12:18:50 25 4
gpt4 key购买 nike

我正在为 Firebase 和 Firestore 使用 Node SDK。

当我使用 collection.add() 时,我使用以下方法在我的文档上设置时间戳:

firebase.firestore.FieldValue.serverTimestamp()

我的问题是我正在使用 collection.onSnapshot 监听更改,并且时间戳被返回为 null,因为我相信报告的更改是本地更改,即。 db 还没有时间写时间戳。

我相信 firebase.firestore.onSnapshotOptions被添加来解决这个问题,即。您可以设置为 'estimate' 以在本地更改的快照中返回估计的时间戳 - 稍后在服务器更改中返回的实际时间戳。

我的问题是,如何/在我的应用程序中设置此选项?

最佳答案

我遇到了同样的问题并找到了解决方案。您在将快照转换为数据时设置选项。参见示例。

db.collection("cities")
.get()
.then(function(querySnapshot) {
querySnapshot.docChanges().forEach(function(doc) {
// Use the server timestamps in the .data() method
console.log(doc.id, " => ", doc.data({ serverTimestamps: 'estimate' }));
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
我会说文档在这方面不是很清楚,并已发送反馈以查看他们是否可以添加示例。

关于node.js - Firebase Cloud Firestore 如何设置 snapShotOptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54724588/

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