Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to access cloud firestore documents from cloud functions #1656

Open
maheshj01 opened this issue Dec 21, 2024 · 0 comments
Open

Unable to access cloud firestore documents from cloud functions #1656

maheshj01 opened this issue Dec 21, 2024 · 0 comments

Comments

@maheshj01
Copy link
Member

maheshj01 commented Dec 21, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants