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

3 messages · Page 1 of 1 (latest)

brave mulch
#

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;
    }
  );
};
carmine badgeBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      ✅ You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)
trail hill
#

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