我有一个计划函数 (
scheduledFunction
),它调用另一个函数 (_clearOldReports
),该函数使用 collectionGroup 定期从名为 reports
的任何集合中删除文档。
在 GCP 控制台日志查看器中,我得到
Error: 9 FAILED_PRECONDITION
,但没有对解决问题有用的更多详细信息。这个功能过去是有效的,所以我不确定发生了什么变化。
我不知道该尝试什么,因为这个功能已经到位并且已经工作多年了。仅 Node 版本发生了变化,现在为 v20。我们所有的功能都是 V1/1st Gen。
exports.scheduledFunction = functions.pubsub
.schedule("every 30 minutes")
.onRun(async () => {
return Promise.all([_clearOldDocuments()]);
});
async function _clearOldDocuments() {
const date = Date.now() - 600000; // 10 min
const reports = await admin
.firestore()
.collectionGroup("reports")
.where("generateDate", "<", date)
.get();
const batch = admin.firestore().batch();
reports.forEach((d) => batch.delete(d.ref));
await batch.commit();
}
我想我找到了错误原因,所以请尝试让我知道。
async function _clearOldDocuments() {
const date = Date.now() - 600000; // 10 min
const reportsSnapshot = await admin
.firestore()
.collectionGroup("reports")
.where("generateDate", "<", date)
.get(); //<- it is not the expected doc "reports" it is querySnapshot
const batch = admin.firestore().batch();
reportsSnapshot.docs.forEach((doc) => batch.delete(doc.ref));
await batch.commit();
}