-
Notifications
You must be signed in to change notification settings - Fork 206
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
Labels
Comments
Hi @maheshj01, Before I look into this issue, did you ever figure out the problem or find a workaround? |
@CorieW I haven't been able to figure out the problem |
I managed to get this working with the following code: /* eslint-disable max-len */
import * as admin from "firebase-admin";
import * as functions from "firebase-functions";
const app = admin.initializeApp({
credential: admin.credential.applicationDefault(),
});
const db = app.firestore();
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);
}
}
); Could you give it a try or let me know if there's any further issues? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
I tried using v2 and v1 both but with no success to retrieve the firestore documents in cloud functions
Related issues
[REQUIRED] Version info
node:
firebase-functions:
firebase-tools:
firebase-admin:
[REQUIRED] Test case
[REQUIRED] Steps to reproduce
V1
V2
[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
The text was updated successfully, but these errors were encountered: