I have a strange problem where for some reason my query from a cloud function does not work, but when I am doing it from the query builder, it does work.
The data in firestore looks like this:
The query has to look like in the query builder, I have added a picture below:
Offcourse, I was prompted to create the composite index, I created using the URL that was provided by query builder:
And now I need to create a query like this in a cloud function.
I created the code like so:
import { App, initializeApp } from "firebase-admin/app"
import { Firestore, Timestamp, getFirestore } from 'firebase-admin/firestore';
import * as logger from "firebase-functions/logger";
import { ScheduleOptions, onSchedule } from 'firebase-functions/v2/scheduler';
import { REGION } from '../configuration/set_up';
const scheduleDetails: ScheduleOptions = {
region: REGION,
schedule: "0 1 * * SUN",
};
export const firebaseApp: App = initializeApp();
export const accAutoRemovalScheduler = onSchedule(scheduleDetails, async (event) => {
logger.info(`Starting search for accounts that require removal`);
logger.info(`Event: ${event.jobName}`);
const firestore: Firestore = getFirestore(firebaseApp);
const currentTimestamp = Timestamp.now();
logger.info(currentTimestamp);
const accountsToRemove = await firestore.collection("accounts")
.where("deletedAccount", "==", false)
.where("manageManually", "==", false)
.where("gracePeriodEndDate", "<", currentTimestamp)
.get();
if (accountsToRemove.empty) {
logger.info("No Accounts to remove");
return;
}
logger.info("Amount of accounts to remove:" + accountsToRemove.size);
accountsToRemove.forEach(
(x) => {
logger.info(x.data());
}
);
logger.info(`Finishing search for accounts that require removal`);
});
I have two problems with this code:
- It tells me again that I need to create a composite index, but, there is one already created. Creating a new one using the link they provide, creates a new identical index. Not sure what am I missing here.
This is the index that it is sugesting me to add: - If I create the index as they tell me, the code works but it returns no value. I know for sure it should return some data.
Can somebody help me with some sugestions on what am I doing wrong here?