mbr*_*iff 6 node.js firebase google-cloud-firestore
我正在使用适用于 Firebase 和 Firestore 的 Node SDK。
当我使用 collection.add() 时,我使用以下方法在文档上设置时间戳:
firebase.firestore.FieldValue.serverTimestamp()
Run Code Online (Sandbox Code Playgroud)
我的问题是我正在使用 collection.onSnapshot 监听更改,并且时间戳返回为空,因为我相信报告的更改是本地更改,即。数据库还没有时间写入时间戳。
我相信添加了firebase.firestore.onSnapshotOptions来解决这个问题。您可以设置为“估计”,以便在本地更改的快照中返回估计时间戳 - 稍后在服务器更改中返回的实际时间戳。
我的问题是,如何/在哪里在我的应用程序中设置此选项?
我遇到了同样的问题并找到了解决方案。您在将快照转换为数据时设置选项。参见示例。
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);
});
Run Code Online (Sandbox Code Playgroud)
我想说的是,文档在这方面并不是很清楚,并且已发送反馈以查看他们是否可以添加示例。
Fra*_*len -1
据我所知,这种类型仅用于QuerySnapshot.docChanges(). 在这种情况下,它的用法应该是这样的:
db.collection("yourcollection")
.get()
.then(function(querySnapshot) {
querySnapshot.docChanges({ serverTimestamps: 'estimate' }).forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
Run Code Online (Sandbox Code Playgroud)
有效值为serverTimestamps:"estimate"、"previous"和"none"(这是默认值)。有关其含义的详细信息,请参阅的文档。SnapshotOptions. serverTimestamps
| 归档时间: |
|
| 查看次数: |
1351 次 |
| 最近记录: |