无法在 Google Cloud Pub/Sub 函数中按日期从 Firebase 获取文档
Unable to fetch documents by date from Firebase in Google Cloud Pub/Sub Function
我有一个 pub/sub 函数 运行,当它被触发时,它会从今天 date
所在的 Firebase 集合中获取所有文档。
在本地(非pubsub)节点环境下测试代码,完美运行,但发布时,找不到任何文档,但在本地版本中可以。
我猜这与已通过的日期有关,但我不明白为什么它可以在本地工作,但不能在这个 pub/sub 函数中工作?
export const check_orders = functions.pubsub.schedule('0 9 * * *').timeZone('Europe/London').onRun(async context => {
const currentDate = moment();
currentDate.set({hour: 0, minute: 0, second: 0, millisecond: 0});
console.log('currentDate:', currentDate);
const toDate = currentDate.toDate();
console.log('toDate:', toDate);
const currentDateTimestamp = admin.firestore.Timestamp.fromDate(currentDate.toDate());
console.log('currentDateTimestamp:', currentDateTimestamp);
const query = db.collection('orders').where('delivery.delivery_due', '==', toDate).where('user_status', '==', 'active');
const tasks = await query.get();
...
}
触发时,currentDate
将记录:moment("2020-05-01T00:00:00.000")
我也试过将 toDate()
添加到 currentDate
,这将记录 2020-05-01T00:00:00.000Z
。也找不到任何文件。
添加了另一个测试以将日期转换为输出 Timestamp { _seconds: 1588291200, _nanoseconds: 0 }
的 Firebase 时间戳,但仍未获取任何文档。
如果我在本地测试中循环上述代码,我会得到 1 个文档,但 pub/sub 不会 return 任何。测试仅删除 status = active
处的获取文档并且成功,因此肯定与日期有关。
测试文档delivery_due
日期设置为今天,user_status
设置为active
,所以文档中的数据符合预期。
我是不是漏掉了什么?
您遇到时区差异问题。你从那一刻开始的日期是在 UTC 测量的(这就是 "Z" 在记录的 currentDate
字符串末尾的意思),但是你在时间戳字段中的日期是在 UTC+1 测量的。他们都说 "midnight",但由于他们在不同的时区,他们指的不是同一时刻,所以您的查询不匹配。
您需要注意确保在查询和文档中使用准确 相同的时间。我还建议查看在时间戳中插入日期的代码,以使其与查询所需内容相匹配。
我有一个 pub/sub 函数 运行,当它被触发时,它会从今天 date
所在的 Firebase 集合中获取所有文档。
在本地(非pubsub)节点环境下测试代码,完美运行,但发布时,找不到任何文档,但在本地版本中可以。
我猜这与已通过的日期有关,但我不明白为什么它可以在本地工作,但不能在这个 pub/sub 函数中工作?
export const check_orders = functions.pubsub.schedule('0 9 * * *').timeZone('Europe/London').onRun(async context => {
const currentDate = moment();
currentDate.set({hour: 0, minute: 0, second: 0, millisecond: 0});
console.log('currentDate:', currentDate);
const toDate = currentDate.toDate();
console.log('toDate:', toDate);
const currentDateTimestamp = admin.firestore.Timestamp.fromDate(currentDate.toDate());
console.log('currentDateTimestamp:', currentDateTimestamp);
const query = db.collection('orders').where('delivery.delivery_due', '==', toDate).where('user_status', '==', 'active');
const tasks = await query.get();
...
}
触发时,currentDate
将记录:moment("2020-05-01T00:00:00.000")
我也试过将 toDate()
添加到 currentDate
,这将记录 2020-05-01T00:00:00.000Z
。也找不到任何文件。
添加了另一个测试以将日期转换为输出 Timestamp { _seconds: 1588291200, _nanoseconds: 0 }
的 Firebase 时间戳,但仍未获取任何文档。
如果我在本地测试中循环上述代码,我会得到 1 个文档,但 pub/sub 不会 return 任何。测试仅删除 status = active
处的获取文档并且成功,因此肯定与日期有关。
测试文档delivery_due
日期设置为今天,user_status
设置为active
,所以文档中的数据符合预期。
我是不是漏掉了什么?
您遇到时区差异问题。你从那一刻开始的日期是在 UTC 测量的(这就是 "Z" 在记录的 currentDate
字符串末尾的意思),但是你在时间戳字段中的日期是在 UTC+1 测量的。他们都说 "midnight",但由于他们在不同的时区,他们指的不是同一时刻,所以您的查询不匹配。
您需要注意确保在查询和文档中使用准确 相同的时间。我还建议查看在时间戳中插入日期的代码,以使其与查询所需内容相匹配。