"error": "Failed to fetch interpretations"

13 views Asked by At

I'm attempting to retrieve data from the Appwrite database. The process is successful when using a POST request in Postman, but encounters an error when using a GET request. Any help?

I received this error using GET request. It says server error. Not really sure about that. I'm still a beginner.

Error fetching interpretation AppwriteException: Server Error
    at Client.eval (webpack-internal:///(rsc)/./node_modules/appwrite/dist/esm/sdk.js:448:27)
    at Generator.next (<anonymous>)
    at fulfilled (webpack-internal:///(rsc)/./node_modules/appwrite/dist/esm/sdk.js:51:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 500,
  type: 'general_unknown',
  response: {
    code: 500,
    type: 'general_unknown',
    version: '0.12.111'
  }
}

Here is what I entered in Postman:

enter image description here

And here is my source code for creating and fetching data.

import client from "@/lib/appwrite_client";
import { ID, Databases, Query } from "appwrite";
import { NextResponse } from "next/server";

const database = new Databases(client);

//Create Interpretation
async function createInterpretation(data: {term: string, interpretation: string}) {
    try {
        const response = await database.createDocument(
            process.env.NEXT_PUBLIC_APPWRITE_DATABASE_ID as string,
            "Interpretations",
            ID.unique(),
            data
        );
        return response;
    } catch (error) {
        console.error("Error creating interpretation", error);
        throw new Error("Failed to create interpretation");
    }
}

//Fetch Interpretation
async function fetchInterpretations() {
    try {
        const response = await database.listDocuments(
            process.env.NEXT_PUBLIC_APPWRITE_DATABASE_ID as string,
            "Interpretations",
            [Query.orderDesc("$createdAt")]
        );
        return response.documents;
    } catch (error) {
        console.error("Error fetching interpretation", error);
        throw new Error("Failed to fetch interpretation");
    }
}

//POST
export async function POST(req: Request) {
    try {
        const {term, interpretation} = await req.json();
        const data = { term, interpretation };
        const response = await createInterpretation(data);
        return NextResponse.json({ message: "Interpretation created "});

    } catch(error) {
        return NextResponse.json(
            {
                error: "Failed to create interpretations",
            },
            { status: 500 }
        );
    }
}

//GET
export async function GET() {
    try {
        const interpretations = await fetchInterpretations();
        return NextResponse.json(interpretations);
    } catch(error) {
        return NextResponse.json(
            { error: "Failed to fetch interpretations" },
            { status: 500 }
        );
    }
}

0

There are 0 answers