skip to Main Content

How can I get in my API the slug passed (slug=${params.slug},`)
I’m working with vercel AI SDK

  const { messages, input, handleInputChange, handleSubmit, isLoading, error } =
    useChat({
      api: `/api/conversation?slug=${params.slug}`,
    });

My API / Not getting the slug:

export async function POST(req: Request) {
  try {
    const { userId } = auth();
    const body = await req.json();
    const { messages, slug } = body;

2

Answers


  1. Chosen as BEST ANSWER

    What I tried but not working, I only get the error:

    export async function POST(req: Request) {
      try {
        const { userId } = auth();
        const body = await req.json();
        const { messages } = body;
        const { chatId } = req.params;
    
        return NextResponse.json(chatId);
      } catch (error) {
        console.log("[CONVERSATION_ERROR]", error);
        return NextResponse.json({
          error: "An error occurred while processing the request.",
        });
      }
    

    for that call :

      const {
        messages,
        setMessages,
        input,
        handleInputChange,
        handleSubmit,
        isLoading,
        error,
      } = useChat({
        api: `/api/conversation?chatId=${params.slug}`,
      });
    

  2. You can use req.query for accessing the query string.

    Or use req.params by keeping the template of api as /api/conversation/:slug
    and you will get something like console.log(req.params) // Output: {slug: 'Your Slug'}

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search