#GET Function on App Router

19 messages · Page 1 of 1 (latest)

oblique remnant
#

This is my code:

export async function GET(req) {
    try {
      const email = await req.query.email;
      if (!email) {
        return NextResponse.json({ message: "Email query parameter is required." }, { status:  400 });
      }
      return NextResponse.json({ message: `Email received: ${email}` }, { status:  200 });
    } catch (error) {
      console.error(error);
      return NextResponse.json({ error: error, message: "Encountered an error" }, { status:  500 });
    }
  }

But for some reason, the value of email is always undefined. The API endpoint I'm using is /api/[email protected]. I don't have any config for bodyParser. I even tried adding the config but still returning as undefined. But for some reason, from the same file, POST function works fine using await req.json().

I've been dealing this for days and I don't know why it's returning as undefined. I tried checking req.query but still undefined.

Thank you in advance

river urchinBOT
#

🔎 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)

oblique remnant
#

This is the response I'm getting:

{
"error": {},
"message": "Encountered an error"
}

languid cipher
#

Try printing the req.query object and the req.url to check it out

#

Also you can send the email inside the request header or body instead of doing this

minor zephyr
#

I'm doing it like this:

export function POST(request: NextRequest) {
  const tag = request.nextUrl.searchParams.get("tag");
  ...
}
oblique remnant
oblique remnant
#

Btw, I'm using js instead of typescript.

languid cipher
oblique remnant
#

Yes correct. So by default, my API endpoint is correct and I'm getting the data. But for some reason, the GET function doesn't seem to get the value of my query. I'm not really sure what's the next step for this.

minor zephyr
oblique remnant
#

Thank you, For some reason, this returns the correct value: const test = await req.nextUrl.searchParams.get("email");. May I ask if why req.query is not returning any value but req.nextUrl returns a value?

I'm sorry, I'm new with the new features of Nextjs

minor zephyr
#

Can you try this?

export async function GET(req) {
  try {
    const email = req.nextUrl.searchParams.get("email");
    if (!email) {
      return NextResponse.json(
        { message: "Email query parameter is required." },
        { status: 400 }
      );
    }
    return NextResponse.json(
      { message: `Email received: ${email}` },
      { status: 200 }
    );
  } catch (error) {
    console.error(error);
    return NextResponse.json(
      { error: error, message: "Encountered an error" },
      { status: 500 }
    );
  }
}
#

May I ask if why req.query is not returning any value but req.nextUrl returns a value?
No idea actually, sorry!

oblique remnant
# minor zephyr Can you try this? ```js export async function GET(req) { try { const emai...

This works. Thank you so much! Here's my updated file:

export async function GET(req) {
    try {
      const email = await req.nextUrl.searchParams.get("email");
      if (!email) {
        return NextResponse.json({ message: "Email query parameter is required." }, { status:  400 });
      }

      const user = await prisma.user.findUnique({
        where: { email: email },
      });

      return NextResponse.json({ message: `Email received: ${email}`, user: user }, { status:  200 });
    } catch (error) {
      console.error(error);
      return NextResponse.json({ error: error, message: "Encountered an error" }, { status:  500 });
    }
  }
river urchinBOT
#
✅ Success!

This question has been marked as answered! If you have any other questions, feel free to create another post

Jump to answer
oblique remnant
#

Thank you so much @minor zephyr and @languid cipher for taking time in checking my concern! Really appreciate it!