'Issue with scheduled function. Error: Value for argument "value" is not a valid query constraint. Cannot use "undefined" as a Firestore value

I am writing a cloud function that will move expired events from a collection to another. It is not working as expected and I am very novice at Javascript. Please save me.

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;
  });
});

This is the next part of the error. I tried with a collection with no documents and a few documents, but I am starting to think that because none of them have expired yet, that's why I am getting this error?

Rest of error log

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)


Solution 1:[1]

You've at least two problems:

  1. I think you want a JavaScript Date object (not Firebase Timestamp object) in your query, i.e. where('endDate', '<=', new Date())
  2. Firebase Timestamp doesn't have a millisecondsSinceEpoch property which is -- I think -- causing the "undefined" error that you're encountering.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DazWilkin