Next.js Discord

Discord Forum

API returns html

Unanswered
Ojos Azules posted this in #help-forum
Open in Discord
Ojos AzulesOP
Do you know why my api route returns html instead of json?

import prismadb from "@/lib/prismadb";
import { NextResponse } from "next/server";


export async function GET(
    req: Request,
    { params }: { params: { storeId: string } }
) {
    
    try {

        if (!params.storeId) {
            return new NextResponse("Store ID is required", { status: 400 });
        }

        const news = await prismadb.news.findMany({
            where: {
                storeId: params.storeId,
            },
            orderBy: {
                createdAt: 'desc'
            },
            include: {
                images: true,
            }
        });

        return NextResponse.json(news);
    } catch (error) {
        console.log('[NEWS_GET]', error);
        return new NextResponse("Internal Error", { status: 500 });
    }
}   

7 Replies

Brown bear
The code itself looks fine. Is the file in the correct place?
i.e.
/app/api/news/[storeId]/route.ts
Ojos AzulesOP
its /app/api/news/[storeId]/news/route.ts
@Brown bear
Ojos AzulesOP
but if click the url i will get that
@Ojos Azules Click to see attachment
you get this data when you call NextResponse.json, rest of your calls are just NextResponse which returns text/html
Ojos AzulesOP
So what I have to do?