首页 > 解决方案 > 我们如何使用 Angular 按 TimeStamp 数据类型对 Firebase Firestore 数据进行排序?

问题描述

我正在使用 Ticket Posting Web 应用程序,我只会显示那些有效且在 24 小时内(从现在开始)记录内的票。

我正在使用Firebase Cloud FirestoreAngular 7

 getTicketList(){
 this.ticketData = 
 this.fireStore.collection('ticket').doc("paid").collection("ticket-list", 
 ref =>
 ref.where('status', '==', 
  "active").where("paid","==","false").orderBy("myTimeStamp");
 return this.ticketData.snapshotChanges() ;
 }

我们有一个键“myTimestamp”,它是Firestore 时间戳类型。

只想显示那些处于活动状态且当前还有时间预订的记录。

Beow 我提到了我用来从 Firebase 服务获取的代码。

 this.firestore.getTicketList().subscribe(list => {
  this.ticketList = list.map(item => {
    return { id:item.payload.doc.id,
      ...item.payload.doc.data()}
  })
  console.log(this.ticketList);
});

谢谢

已编辑

在此处输入图像描述 在此处输入图像描述

标签: node.jsangularfirebasetimestampgoogle-cloud-firestore

解决方案


您可以通过传递Date对象来查询数据/时间戳字段。因此,例如现在之前获取所有项目:

ref
  .where('status', '==', "active")
  .where("paid","==","false")
  .where("myTimeStamp","<", new Date())
  .orderBy("myTimeStamp")

推荐阅读