繁体   English   中英

如何使用 React 以带有时间戳的 firestore 顺序检索文档

[英]How to retrieve documents in firestore order with timestamp with React

我写了下面的代码。

const salons = query(collection(db, "user"), orderBy('', "desc"), limit(5))
const querySnapshot = await getDocs(salons)
await querySnapshot.forEach((doc) => {
    console.log(new Date(doc._document.version.timestamp.seconds * 1000).toString())
    //processing
})

我想按 console.log 中显示的“doc._document.version.timestamp.seconds”对 orderby 进行排序。

但我不知道该怎么做。 这个日期是firestore自动注册的,所以值的position是不一样的。 有谁知道我该如何排序?

Firestore 只能根据它具有索引的值来排序/过滤数据。 索引仅为文档中的字段(和文档 ID)创建,而不是为隐式元数据创建,例如它在内部保留的时间戳。

无法根据内部时间戳对 Firestore 结果进行排序。 如果您希望能够根据时间戳对文档进行排序,则必须将该时间戳存储为文档中的一个字段,然后将该字段名称传递给orderBy

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM