本文介绍了如何在 Firestore 中按日期正确执行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个 cronjob 来删除使用 http 触发器传递的事件.今天是 9 月 7 日,所以 9 月 5 日的事件 ( 6fqmROcWD7K1pTFtXmJJ
) 如下图所示将在查询快照中捕获.但文档长度为零
I am trying to make a cronjob to delete an event that has been passed by using http trigger. today is 7th September, so the event at 5 september ( 6fqmROcWD7K1pTFtXmJJ
)like the picture below shall be captured in the query snapshot. but the document length is zero
这是我使用的代码:
export const cronJobDeletingPastEvents = functions.https.onRequest(async (request,response) => {
const dateNow = Date.now()
const pastEventsSnapshot = await admin.firestore().collection("events").where('dateTimeFinish', '<', dateNow).get()
console.log(pastEventsSnapshot.docs.length) // <---- the result is zero
// perform some actions ....
}
我不知道这里出了什么问题,但我想这是因为 dateNow
是数字,但 firestore 中的字段 dateTimeFinish
是 Timestamp代码>对象.但我不知道如何解决这个问题
I don't know what went wrong in here, but I guess this is because dateNow
is number, but the field dateTimeFinish
in firestore is Timestamp
object. but i dont know how to fix that
推荐答案
以下将起作用:
const dateNow = new Date()
const pastEventsSnapshot = await admin.firestore().collection("events").where('dateTimeFinish', '<', dateNow).get()
...
以下方法也可以:
const dateNow = new Date('2018-09-07T01:25:54.000Z')
const dateNow = new Date('2018-09-07')
这篇关于如何在 Firestore 中按日期正确执行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!