Next.js Discord

Discord Forum

Cannot get the contents of req in next.js API route

Unanswered
Briquet Griffon Vendéen posted this in #help-forum
Open in Discord
Briquet Griffon VendéenOP
0

I am using 13.4.5 of next.js. I am using API Route. Here is the code for (1). I am trying to get data by hitting the code in (1) using swr. But when I hit "http://localhost:3001/api/company/123/users?user_id=12" in the url but I can't get the groupId. Please let me know if anyone can help me understand.
â‘ 
// app/api/company/[groupId]/users/route.ts
// route.ts
import { NextRequest, NextResponse } from "next/server";
import { parse } from "url";
import { apiClient } from "@/libs/axios";

export async function GET(req: NextRequest) {
  const { query } = parse(req.url, true);
  const { userId,groupId } = query as any;

  return NextResponse.json({ query });

  // const response = await apiClient.get<Graphs>(
  //   `/company/${groupId}/users?user_id=${userId}`,
  //   {
  //     params: { user_id: userId },
  //   }
  // );
  // return NextResponse.json(response.data);
}

â‘¡
import useSWR from "swr";
import { Items } from "@/generated/models";
import { apiClient } from "@/libs/axios-client";

export const useFetchItems = (groupId: string, userId: string) => {
  return useSWR<Items>(
    `api/company/${groupId}/users?user_id=${userId}`,
    async (url: string, userId: string) => {
      const response = await apiClient.get<Items>(url, {
        params: { user_id: userId },
      });
      return response.data;
    }
  );
};

2 Replies

you are trying to get a group id which is a param from the same place as searchParams. they are different