繁体   English   中英

如何使用时间戳使用来自 Firestore 的查询

[英]How to use query from firestore using timestamp

我正在使用日期范围选择器,让我的用户设置他们将从 Firestore 查询的数据的日期范围。 我已经可以获取日期范围选择器的值并将其从变量中存储。 我的问题是我不知道如何查询 firestore 引用时间戳。

这是我从 Firestore 集合中查询文档的功能。 到目前为止,我只能收集所有数据,但无法收集过滤数据(按时间戳)。

export async function Getfs(){
console.log("Get firestore run >>");
var f = document.getElementById("firstDate").value; // starting date
var s = document.getElementById("secondDate").value; // ending date
const fs_snap = await getDocs(query(collection(fs, "sens02"),orderBy('ts')));
}

这就是我的 Firestore 文档的样子

这就是我使用 html 输入日期范围选择器设计日期范围选择器的方式

您需要使用Timestamp类的方法。

更准确地说,在您的情况下,您需要使用fromDate() ,如下例所示:

const q = query(
  collection(fs, "sens02"),
  where('ts', '>=', Timestamp.fromDate(new Date('2022-05-28'))) // For example we use the 28/05/2002
);

const querySnapshot = await getDocs(q);

querySnapshot.forEach((doc) => {
  console.log(doc.data().ts.toDate());
});

暂无
暂无

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

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