簡體   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