Back

Server error when creating multiple documents

  • 0
  • Databases
  • Web
tinfoilht
30 Jan, 2025, 18:18

I am attempting to create 12 documents in my games collection I am successfully able to create 5 and then there is a server error on 6 it will create 5 more and a server erron on 12.

Is there a way to make this create all 12 games?

TL;DR
Issue: Server error occurs when attempting to create the 6th and 12th documents out of 12 in a games collection. Solution: The error is likely due to an issue with the database operation. You can try implementing error handling strategies like retrying the failed attempts or investigating the root cause of the server error for a more permanent fix.
tinfoilht
30 Jan, 2025, 18:20

this is the terminal output Game 1 created successfully. Game 2 created successfully. Game 3 created successfully. Game 4 created successfully. Game 5 created successfully. Error creating game 6: AppwriteException: Server Error

119 | if (gameData) { 120 | try {

121 | await databases.createDocument( | ^ 122 | process.env.NEXT_PUBLIC_APPWRITE_DATABASE!, 123 | process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_LEAGUE_GAMES!, 124 | ID.unique(), { code: 500, type: 'general_unknown', response: [Object] } Game 7 created successfully. Game 8 created successfully. Game 9 created successfully. Game 10 created successfully. Game 11 created successfully. Error creating game 12: AppwriteException: Server Error

tinfoilht
30 Jan, 2025, 18:21

and this is the code we are using to process the createDocument

for (let gameNumber = 1; gameNumber <= 12; gameNumber++) { const gameData = createGameData( gameNumber, newMatchup.$id, team1Players, team2Players ); if (gameData) { try { await databases.createDocument( process.env.NEXT_PUBLIC_APPWRITE_DATABASE!, process.env.NEXT_PUBLIC_APPWRITE_COLLECTION_LEAGUE_GAMES!, ID.unique(), gameData ); console.log(Game ${gameNumber} created successfully.); } catch (error) { console.error(Error creating game ${gameNumber}:, error); // Handle the error, perhaps skip this game or try again later } } }

TypeScript
return { success: true, matchup: newMatchup };

} catch (error) { console.error("Error creating league matchup or games:", error); return { error: "Failed to create matchup or games. Please try again.", }; } }

// Helper function to create game data based on game number function createGameData( gameNumber: number, matchupId: string, team1Players: string[], team2Players: string[] ) { const gameStructure = { 1: { team1: [0, 2], team2: [0, 2] }, 2: { team1: [1, 3], team2: [1, 3] }, 3: { team1: [0, 3], team2: [0, 3] }, 4: { team1: [1, 2], team2: [1, 2] }, 5: { team1: [0, 1], team2: [2, 3] }, 6: { team1: [2, 3], team2: [0, 1] }, 7: { team1: [0, 2], team2: [1, 3] }, 8: { team1: [1, 3], team2: [0, 2] }, 9: { team1: [0, 3], team2: [1, 2] }, 10: { team1: [1, 2], team2: [0, 3] }, 11: { team1: [0, 1], team2: [0, 1] }, 12: { team1: [2, 3], team2: [2, 3] }, };

const { team1, team2 } = gameStructure[gameNumber]; return { matchupId: matchupId, team1: [team1Players[team1[0]], team1Players[team1[1]]], team2: [team2Players[team2[0]], team2Players[team2[1]]], }; }

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more