mirror of
https://github.com/xavidop/cardex.git
synced 2026-08-29 01:01:19 +00:00
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
This commit is contained in:
+41
-6
@@ -1,3 +1,4 @@
|
||||
|
||||
import {
|
||||
collection,
|
||||
addDoc,
|
||||
@@ -26,18 +27,52 @@ export const addCardToCollection = async (
|
||||
userId: string,
|
||||
cardData: Omit<PokemonCard, 'id' | 'userId' | 'createdAt' | 'updatedAt'>
|
||||
): Promise<string> => {
|
||||
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
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user