如何在云功能上按日期查询?



我试图查询两个日期之间的一些数据,但似乎没有任何工作。这些是我的日期,我在一个帖子上看到除以1000应该是正确的,但事实并非如此。我试过firebase。firestore,但是firebase没有定义但我不知道如何直接引用firebase。firestore。timestamp

let now = new Date()
let yesterday = Math.round((new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1).getTime())/1000)
now = Math.round(now.getTime()/1000)

这是我的查询尝试。没有任何工作,它只是返回我的空数组,因为查询没有数据来遍历forEach。

let snapshot = await db.collection('appointments')
.where('status', '==', 'Pending')
.where('startDate', '<=', now)
.where('startDate', '>', yesterday)
.get().then(docs => {
docs.forEach(snapshot => {
console.log(snapshot.id, '=>', snapshot.data());
console.log(snapshot.data()['doctor']);
doctor_mail.push(snapshot.id)
doctor_mail.push(snapshot.data())
});
return doctor_mail
}).
catch(err => {
return res.send(err)
});
console.log(doctor_mail)
res.send(snapshot)

查询没有返回任何内容,因为没有什么可返回的。如果您正在查询表示日期的字符串,并且数据是Firestore中的时间戳,那么为了了解这一点,需要检查示例文档进行比较,因为在您的问题的第一部分中您提到您想要获得Firestore时间戳,您可以使用以下代码:

const timestamp = db.FieldValue.serverTimestamp();
//if you want it as a date object
const date = timestamp.toDate();

根据您在评论中提出的问题,要在时间戳中获取Today和Yesterday的值,您可以执行以下操作:

var todayTimestamp = timestamp.now();
var date = new Date().setDate(date.getDate() - 1);
var yesterdayTimestamp = timestamp.fromDate(date);

并将它们转换回日期,以便您可以在需要时操作它们,您可以在此文档中查看时间戳的更多详细信息

这里的问题是我错误地将db调用为const db = firebase.firestore.

我只需要输入:

const db = admin.firestore()

最新更新