#Type error: Route "app/api/storeUser/route.ts" does not match the required types of a Next.js Route.

1 messages · Page 1 of 1 (latest)

umbral crest
#

import { sql } from "@vercel/postgres";
import { NextApiRequest, NextApiResponse } from "next";
import { auth } from "@/auth";
export default async function GET(req: NextApiRequest, res: NextApiResponse) {
const session = await auth();

if (!session) {
res.status(401).json({ error: "Not authenticated" });
return;
}

const userEmail = session!.user!.email;

try {
// fetching the user's email from the PostgreSQL table
const data = await sqlSELECT * FROM user_data WHERE Email='${ session!.user!.email }';
if (data.rows.length === 0) {
try {
await sqlINSER INTO user_data (email) VALUES (${ session!.user!.email });
} catch (error) {
console.error("Error storing email:", error);
res
.status(500)
.json({ error: "An error occurred while storing email" });
}
}

res.status(200).json({ message: "Email stored successfully" });

} catch (error) {
console.error("Error retreiving user: ", error);
res.status(500).json({ error: "An error occurred while retreiving user" });
}
}

wet siloBOT
#

Hi @umbral crest. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The error you're encountering is likely due to the fact that you're using the NextApiRequest and NextApiResponse types in a file that's not located in the pages/api directory. In Next.js, API Routes should be created inside the pages/api directory. Files outside this directory will not be treated as API routes.

In your case, the file app/api/storeUser/route.ts should be moved to pages/api/storeUser/route.ts.

Also, it's important to note that the GET function you're exporting should be the default export if it's an API route. So, you should change export default async function GET(req: NextApiRequest, res: NextApiResponse) to export default async function (req: NextApiRequest, res: NextApiResponse).

Here's how your code should look after the changes:

import { sql } from "@vercel/postgres";
import { NextApiRequest, NextApiResponse } from "next";
import { auth } from "@/auth";

export default async function (req: NextApiRequest, res: NextApiResponse) {
  const session = await auth();

  if (!session) {
    res.status(401).json({ error: "Not authenticated" });
    return;
  }

  const userEmail = session!.user!.email;

  try {
    // fetching the user's email from the PostgreSQL table
    const data = await sql`SELECT * FROM user_data WHERE Email='${
      session!.user!.email
    }'`;
    if (data.rows.length === 0) {
      try {
        await sql`INSER INTO user_data (email) VALUES (${
          session!.user!.email
        })`;
      } catch (error) {
        console.error("Error storing email:", error);
        res
          .status(500)
          .json({ error: "An error occurred while storing email" });
      }
    }
#

res.status(200).json({ message: "Email stored successfully" });
} catch (error) {
console.error("Error retreiving user: ", error);
res.status(500).json({ error: "An error occurred while retreiving user" });
}
}


For more information on how to structure your API routes in Next.js, you can refer to the **Next.js documentation on API Routes** (<https://nextjs.org/docs/api-routes/introduction>).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------