Skip to content
Back

How to update document?

  • 0
  • Databases
tinfoilht
7 Dec, 2024, 01:38

I am lost and have no idea how to fix this

I am trying to update a document with the server SDK in my NextJS applications

this is our // src/app/api/matches/updateMatch/route.ts

try { const { databases, account } = await createSessionClient( sessionCookie.value ); await databases.updateDocument( process.env.NEXT_PUBLIC_DATABASE_ID!, process.env.NEXT_PUBLIC_COLLECTION_MATCHES!, matchId, { [key]: value } );

this is the request

TypeScript
  const response = await fetch(`/api/matches/setup`, {
    method: "PATCH",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ key, value: validatedValue, matchId }),
  });

  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
  }

this is the error Compiled /api/matches/setup in 614ms (849 modules) Session Cookie: eyJpZCI6IjY3MGFkOT... Updating document with: { databaseId: '66------------d0', collectionId: 'matches', matchId: '6745ba27001606a4215b', data: { location: '6712a842000b8bc14916' } } API Error: AppwriteException: Server Error at async PATCH (src\app\api\matches\setup\route.ts:43:4)

41 | }); 42 |

43 | await databases.updateDocument( | ^ 44 | process.env.NEXT_PUBLIC_DATABASE_ID!, // databaseId 45 | process.env.NEXT_PUBLIC_COLLECTION_MATCHES!, // collectionId 46 | matchId, // documentId { code: 500, type: 'general_unknown', response: [Object] } PATCH /api/matches/setup 500 in 1981ms

all of the env variables are correct.

I am lost and don't know what to do next in trouble shooting.

any help would be appreciated

TL;DR
- Developer is having trouble updating a document in a NextJS application using server SDK. - HTTP error occurs when updating document. - Recommendations to log the response body and wrap code in backticks for better formatting. - Solution: Debugging the issue by logging the response body and checking server SDK code for any errors.
Steven
7 Dec, 2024, 01:39

FYI, it's best to wrap code in backticks to format a bit nicer. You can use 1 backtick for inline code (https://www.markdownguide.org/basic-syntax/#code) and 3 backticks for multiline code (https://www.markdownguide.org/extended-syntax/#syntax-highlighting).

Steven
7 Dec, 2024, 01:41

Before throwing that http error, can you try logging the response body?

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