From 4763dc3d5358e412a4ae8784823dc0c038e0b279 Mon Sep 17 00:00:00 2001 From: Xavier Portilla Edo Date: Wed, 4 Jun 2025 13:29:39 +0000 Subject: [PATCH] I am getting this error when saving the card: https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel?gsessionid=Da9ga26vaDS2NMMwCHc18fbJSi5FOsYDhxFGH9WDcJk&VER=8&database=projects%2Fcardex-1mapj%2Fdatabases%2F(default)&RID=rpc&SID=cHzt3lDHgQb2Z_z2VrKW2w&AID=0&CI=0&TYPE=xmlhttp&zx=l9xpcaot2kx5&t=1 --- src/lib/firestore.ts | 47 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/src/lib/firestore.ts b/src/lib/firestore.ts index 4cc5f6e..cdc7dbc 100644 --- a/src/lib/firestore.ts +++ b/src/lib/firestore.ts @@ -1,3 +1,4 @@ + import { collection, addDoc, @@ -26,18 +27,52 @@ export const addCardToCollection = async ( userId: string, cardData: Omit ): Promise => { - if (!userId) throw new Error('User ID is required to add a card.'); + if (!userId) { + console.error("addCardToCollection: User ID is missing."); + throw new Error('User ID is required to add a card.'); + } try { - const docRef = await addDoc(getPokemonCardsCollectionRef(userId), { + const collectionRef = getPokemonCardsCollectionRef(userId); + const docPayload = { ...cardData, - userId, // Ensure userId is part of the document data as well for easier querying if needed + userId, createdAt: serverTimestamp(), updatedAt: serverTimestamp(), - }); + }; + + // Log information for debugging, excluding potentially very large imageDataUrl from general log + const payloadKeys = Object.keys(docPayload); + const imageDataUrlLength = docPayload.imageDataUrl ? docPayload.imageDataUrl.length : 0; + console.log( + "Attempting to add card to Firestore. Path:", + collectionRef.path, + "Payload keys:", + payloadKeys.join(', '), + "imageDataUrl length:", + imageDataUrlLength + ); + + if (imageDataUrlLength > 500 * 1024) { // Warn if image data is > 500KB + console.warn( + `imageDataUrl is very large (length: ${imageDataUrlLength} bytes). This might approach or exceed Firestore's 1MiB document size limit and could cause write failures. Consider storing images in Firebase Storage instead.` + ); + } + + const docRef = await addDoc(collectionRef, docPayload); return docRef.id; } catch (error) { - console.error('Error adding card to Firestore: ', error); - throw new Error('Failed to add card to collection.'); + console.error('Error adding card to Firestore (full error object):', error); // Log the full error object + let detailedMessage = 'Failed to add card to collection.'; + if (error instanceof Error) { + detailedMessage = error.message; // Start with the basic error message + // Attempt to access Firebase-specific error code + // Firebase errors often have a 'code' property, but to be safe with typing, we check its existence. + const firebaseError = error as any; + if (firebaseError.code) { + detailedMessage = `Error: ${firebaseError.code} - ${error.message}`; + } + } + throw new Error(detailedMessage); // Throw a new error with the potentially more detailed message } };