Closed
Description
I tried using v2 and v1 both but with no success to retrieve the firestore documents in cloud functions
Related issues
[REQUIRED] Version info
"firebase-admin": "^13.0.2",
"firebase-functions": "^6.2.0",
node:
firebase-functions:
firebase-tools:
firebase-admin:
[REQUIRED] Test case
[REQUIRED] Steps to reproduce
V1
/* eslint-disable max-len */
import * as admin from "firebase-admin";
import * as functions from "firebase-functions/v1";
admin.initializeApp();
const db = admin.firestore(
functions.config().firebase
);
const logCollection = "logs";
export const updateLogs = functions.pubsub.schedule(
"every 24 hours",
).onRun(async () => {
try {
const totalCollections = await db.listCollections();
console.log(`Checking for expired logs in ${totalCollections.length} collections...`);
const logsCollection = db.collection("logs");
const logsSnapshot = await logsCollection.get();
console.log(`Checking for expired logs... in ${logCollection} against ${logsSnapshot.size} documents`);
if (logsSnapshot.empty) {
console.log("Not found in logs collection....");
return; // Return nothing instead of null
}
const currentDate = new Date();
const batch = db.batch();
logsSnapshot.forEach((doc) => {
const logData = doc.data();
const expiryDateString = logData.expiryDate;
// Ensure expiryDate is correctly parsed
const expiryDate = expiryDateString ? new Date(expiryDateString) : null;
// Convert both dates to UTC to ensure correct comparison
const isExpired = expiryDate ? expiryDate.getTime() <= currentDate.getTime() : false;
console.log(`Doc ID: ${doc.id}, Expiry Date: ${expiryDate}, Current Date: ${currentDate}, Is Expired: ${isExpired}`);
batch.update(doc.ref, { isExpired });
});
await batch.commit();
console.log("Expiry check and update completed.");
return;
} catch (error) {
console.error("Error checking expiry:", error);
}
}
);
V2
/* eslint-disable max-len */
import * as admin from "firebase-admin";
import * as functions from "firebase-functions";
admin.initializeApp();
const db = admin.firestore(
functions.config().firebase
);
const logCollection = "logs";
export const updateLogs = functions.scheduler.onSchedule(
{
schedule: "0 0 * * *", // Runs at midnight UTC every day
timeZone: "Etc/UTC",
retryCount: 3,
memory: "256MiB", // Optional: Customize memory allocation
timeoutSeconds: 60, // Optional: Set timeout for the function
},
async (event) => {
try {
const logsCollection = db.collection("logs");
const logsSnapshot = await logsCollection.get();
console.log(`Checking for expired logs... in ${logCollection} against ${logsSnapshot.size} documents`);
if (logsSnapshot.empty) {
console.log("Not found in logs collection....");
return; // Return nothing instead of null
}
const currentDate = new Date();
const batch = db.batch();
logsSnapshot.forEach((doc) => {
const logData = doc.data();
const expiryDateString = logData.expiryDate;
// Ensure expiryDate is correctly parsed
const expiryDate = expiryDateString ? new Date(expiryDateString) : null;
// Convert both dates to UTC to ensure correct comparison
const isExpired = expiryDate ? expiryDate.getTime() <= currentDate.getTime() : false;
console.log(`Doc ID: ${doc.id}, Expiry Date: ${expiryDate}, Current Date: ${currentDate}, Is Expired: ${isExpired}`);
batch.update(doc.ref, { isExpired });
});
await batch.commit();
console.log("Expiry check and update completed.");
return;
} catch (error) {
console.error("Error checking expiry:", error);
}
}
);
[REQUIRED] Expected behavior
Documents can be fetched from cloud functions without any additional authentication
[REQUIRED] Actual behavior
No collections/documents found
Were you able to successfully deploy your functions?
You can find the complete source code here https://github.com/maheshj01/pastelog/tree/main/functions