#NextResponse API Routes, body ReadableStream: can't read fetched data

7 messages · Page 1 of 1 (latest)

unborn edge
#

Hi everyone,

I'm using Next.js 13 with appDirectory.
I'm experiencing weird issue, I have a route /api/events

app/api/events/routes.ts

import { client } from '@/contentful/setup';
import { NextResponse } from 'next/server';

export async function GET(request: Request) {
    const res = await client.getEntries({
        content_type: 'events' // only fetch blog post entry
    });

    return NextResponse.json(res.items);
}

I can see the json when going to http://localhost/api/events BUT I can't read the json from the client

async function getEvents() {
    const res = await fetch('/api/events', { method: 'GET' });
    return res;
}

export default function Home() {
    // using react-query here
    const { isLoading, isError, data, error } = useQuery({
        queryKey: ['events'],
        queryFn: getEvents
    });

    console.log(data)

    return (<></>)
}

and i got this log and can't read the body.. :

Response {type: 'basic', url: 'http://localhost:3000/api/events', redirected: false, status: 200, ok: true, …}
body: ReadableStream
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
url: "http://localhost:3000/api/events"

Any hint ?

#

NextResponse API Routes, body ReadableStream: can't read fetched data

unborn edge
#

Still have same issue, I have tested without react-query and it doesn't seem to be the problem here. I think there is something wrong with NextResponse.json but dont know what...

icy crescent
#

you can't read data from res, it has to be parsed first with res.json

unborn edge
#

lol i feel stupid to miss this out, was sure to have it set on my fetch 🥲 thanks anyway @icy crescent !