No response returning from api route

I'm making an api route to fetch/create a chatId for a chat room but its not returning any response, what is wrong here?
import type {NextRequest} from "next/server";
import {NextResponse} from "next/server";
import {getAuthSession} from "@/lib/auth";
import {z} from "zod";
import {db} from "@/lib/db";
import {chats} from "@/lib/db/schema";
import {and, eq, or} from "drizzle-orm";

export async function POST(req: NextRequest) {
try {
const session = await getAuthSession();
if (!session) {
return new Response("UNAUTHORIZED", {
status: 401
});
}
const userId = session.user.id;

const body = await req.json();
const {other} = z.object({
other: z.string()
}).parse(body);

const {chatId} = (await db.select({
chatId: chats.id
}).from(chats).where(
or(
and(eq(chats.userId1, userId), eq(chats.userId2, other)),
and(eq(chats.userId1, other), eq(chats.userId2, userId))
)
).limit(1))[0];

// if (chatId) {
// return new Response(JSON.stringify(chatId), {
// status: 200
// });
// }

const res = (await db.insert(chats).values({
userId1: userId,
userId2: other
}).returning({chatId: chats.id}))[0];

// return new Response(JSON.stringify(res.chatId), {
// status: 200
// });
if (chatId !== null) {
return new NextResponse(chatId, {
status: 200
});
} else if (res.chatId) {
return new NextResponse(res.chatId, {
status: 200
});
} else {
return new NextResponse("ERROR", {
status: 500
});
}
} catch (err) {
console.log(err);
}
}
import type {NextRequest} from "next/server";
import {NextResponse} from "next/server";
import {getAuthSession} from "@/lib/auth";
import {z} from "zod";
import {db} from "@/lib/db";
import {chats} from "@/lib/db/schema";
import {and, eq, or} from "drizzle-orm";

export async function POST(req: NextRequest) {
try {
const session = await getAuthSession();
if (!session) {
return new Response("UNAUTHORIZED", {
status: 401
});
}
const userId = session.user.id;

const body = await req.json();
const {other} = z.object({
other: z.string()
}).parse(body);

const {chatId} = (await db.select({
chatId: chats.id
}).from(chats).where(
or(
and(eq(chats.userId1, userId), eq(chats.userId2, other)),
and(eq(chats.userId1, other), eq(chats.userId2, userId))
)
).limit(1))[0];

// if (chatId) {
// return new Response(JSON.stringify(chatId), {
// status: 200
// });
// }

const res = (await db.insert(chats).values({
userId1: userId,
userId2: other
}).returning({chatId: chats.id}))[0];

// return new Response(JSON.stringify(res.chatId), {
// status: 200
// });
if (chatId !== null) {
return new NextResponse(chatId, {
status: 200
});
} else if (res.chatId) {
return new NextResponse(res.chatId, {
status: 200
});
} else {
return new NextResponse("ERROR", {
status: 500
});
}
} catch (err) {
console.log(err);
}
}
2 Replies
jeff.kershner
jeff.kershner14mo ago
It is tough to debug this just by looking at your code. If you write console logs at each step and see what values are going in and coming out, that is probably the best way to see what is going on. It is weird that you are not getting any response? Like seriously no response from the API? Are you getting a 200 but no data, or is the API not even returning?
aditya
aditya14mo ago
I'm getting 500 response ⨯ Error: No response is returned from route handler 'F:\Projects\Web\ninjask-2\src\app\api\buzz\get-chat-id\route.ts'. Ensure you return a Response or a NextResponse in all branches of your handler. i fixed it nvm cannot destructure the first db call it seems
Want results from more Discord servers?
Add your server