Next.js Discord

Discord Forum

NextJS GET request returns empty object

Answered
Bordin posted this in #help-forum
Open in Discord
Hello, my api is returning an empty object even though there is results from the database
public async GetCategories(){
       try{
         let { data: Category, error } = await this.supabase
         .from('Category') 
          .select('*')
          if(error){
             console.log(error);
          }else{
            console.log(Category)//results are available
             return NextResponse.json(Category); //returned {} for some reasons
          }
       }catch(error){
         console.log(error);
       }
       
    }

anyone knows that the issue is?
Answered by American black bear
just return the object in getcategories instead of returning nextresponse.json
View full answer

21 Replies

American black bear
ahh, is category an object?
it prolly is an array
yea
American black bear
try NextResponse.json({categories: Category})
array
still
empty {}
American black bear
can you share your code, where you're using the function
cause nextresponse.json is supposed to be used for api routes :meow_stare:
this doesn't look like an api route
api route == route handlers FYI
export async function GET(){
    const service = new CategoryReposiroty();
    const result = service.GetCategories();
    return NextResponse.json(result);
}
uhm wait leme share the whole code
American black bear
just return the object in getcategories instead of returning nextresponse.json
Answer
American black bear
like a regular object
ey it worked
thannksyou
American black bear
np, if your problem is solved, consider marking the answer as solution
Original message was deleted
American black bear
^
American black bear
it's already done, leave it. don't worry