如何使用 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”排序。

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

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

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