Next.js Discord

Discord Forum

api

Unanswered
Blanc de Hotot posted this in #help-forum
Open in Discord
Blanc de HototOP
hello, im getting req.query empty/undefined

8 Replies

Blanc de HototOP
import { NextResponse } from "next/server";
import dbConnect from "@db/mongo.js";
import Schema from "@model/User.js";

export async function GET(req, res) {
  await dbConnect();
  const { query } = req;

  console.log(query);

  let data = await Schema.findOne({
    name: url.username,
  });

  if (!data) {
    let data = await new Schema({
      name: url.username,
      password: url.password,
      _id: uuid(),
    }).save();

    data = await Schema.findOne({
      name: url.username,
    });
  }

  return NextResponse.json({ data: data ? data : false });
}

function uuid() {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
    const r = (Math.random() * 16) | 0;
    const v = c === "x" ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
}
file:
src/app/api/user/create.js
@Blanc de Hotot hello, im getting req.query empty/undefined
in the app router it is no longer req.query
Blanc de HototOP
then ?
the syntax of route handlers are very very different; it is no longer req, res
read the whole page linked above to see how to use the new syntax
Blanc de HototOP
Thanks a lot the For help!!