Issue with scheduled function. Error: Value for argument "value" is not a valid query constraint. Cannot use "undefined" as a Firestore value
Issue with scheduled function. Error: Value for argument "value" is not a valid query constraint. Cannot use "undefined" as a Firestore value
我正在编写一个云函数,用于将过期事件从一个集合移动到另一个集合。它没有按预期工作,我是 Javascript 的新手。请救救我。
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();
exports.expireEvents = functions.region("us-west2").pubsub.schedule('* * * * *').onRun(async (context) => {
await db.collection("Events").where('endDate', '<=', admin.firestore.Timestamp.now().millisecondsSinceEpoch).get().then((snapshot) => {
snapshot.forEach(async (doc) => {
// For all expired events, we will perform operations on some of their fields
const event = doc.data();
// Write document to collection of "expired events"
await db.collection("ArchivedEvents").doc(event.eid).set(event);
// For all the guests in that expired event, do stuff; guests should be a list of strings.
event.guests.forEach(async (uid) => {
// Create a new write batch
const batch = db.batch();
// Get user and update some attributes
const guest = await db.collection("Users").doc(uid);
// Add all operations to be performed for given user to this batch
batch.update(guest, {eventsAttended: admin.firestore.FieldValue.arrayUnion(event.eid)});
batch.update(guest, {eventsAttending: admin.firestore.FieldValue.arrayRemove(event.eid)});
// Execute batch of operations
await batch.commit();
});
// Delete doc from "not expired" collection
await db.collection("Events").doc(event.eid).delete();
});
console.log(`Successfully expired events ending on ${admin.firestore.Timestamp.now()}.`);
return true;
})
.catch((err) => {
console.error(`Could not get or update documents. Error ${err}.`);
return false;
});
});
这是错误的下一部分。我尝试使用一个没有文档和一些文档的集合,但我开始认为因为 none 其中的文档已经过期,这就是我收到此错误的原因?
错误日志的其余部分
If you want to ignore undefined values, enable `ignoreUndefinedProperties`.
at Object.validateUserInput (/workspace/node_modules/@google-cloud/firestore/build/src/serializer.js:277:19)
at validateQueryValue (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:2230:18)
at CollectionReference.where (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:1061:9)
at /workspace/index.js:139:33
at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:131:23)
at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/function_wrappers.js:144:25
at processTicksAndRejections (node:internal/process/task_queues:96:5)
你至少有两个问题:
- 我想你想要一个 JavaScript
Date
对象(不是 Firebase Timestamp
对象)在你的查询中,即 where('endDate', '<=', new Date())
- Firebase
Timestamp
没有 millisecondsSinceEpoch
属性,我认为这是导致您遇到的“未定义”错误的原因。
我正在编写一个云函数,用于将过期事件从一个集合移动到另一个集合。它没有按预期工作,我是 Javascript 的新手。请救救我。
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();
exports.expireEvents = functions.region("us-west2").pubsub.schedule('* * * * *').onRun(async (context) => {
await db.collection("Events").where('endDate', '<=', admin.firestore.Timestamp.now().millisecondsSinceEpoch).get().then((snapshot) => {
snapshot.forEach(async (doc) => {
// For all expired events, we will perform operations on some of their fields
const event = doc.data();
// Write document to collection of "expired events"
await db.collection("ArchivedEvents").doc(event.eid).set(event);
// For all the guests in that expired event, do stuff; guests should be a list of strings.
event.guests.forEach(async (uid) => {
// Create a new write batch
const batch = db.batch();
// Get user and update some attributes
const guest = await db.collection("Users").doc(uid);
// Add all operations to be performed for given user to this batch
batch.update(guest, {eventsAttended: admin.firestore.FieldValue.arrayUnion(event.eid)});
batch.update(guest, {eventsAttending: admin.firestore.FieldValue.arrayRemove(event.eid)});
// Execute batch of operations
await batch.commit();
});
// Delete doc from "not expired" collection
await db.collection("Events").doc(event.eid).delete();
});
console.log(`Successfully expired events ending on ${admin.firestore.Timestamp.now()}.`);
return true;
})
.catch((err) => {
console.error(`Could not get or update documents. Error ${err}.`);
return false;
});
});
这是错误的下一部分。我尝试使用一个没有文档和一些文档的集合,但我开始认为因为 none 其中的文档已经过期,这就是我收到此错误的原因?
错误日志的其余部分
If you want to ignore undefined values, enable `ignoreUndefinedProperties`.
at Object.validateUserInput (/workspace/node_modules/@google-cloud/firestore/build/src/serializer.js:277:19)
at validateQueryValue (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:2230:18)
at CollectionReference.where (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:1061:9)
at /workspace/index.js:139:33
at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:131:23)
at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/function_wrappers.js:144:25
at processTicksAndRejections (node:internal/process/task_queues:96:5)
你至少有两个问题:
- 我想你想要一个 JavaScript
Date
对象(不是 FirebaseTimestamp
对象)在你的查询中,即where('endDate', '<=', new Date())
- Firebase
Timestamp
没有millisecondsSinceEpoch
属性,我认为这是导致您遇到的“未定义”错误的原因。