AI SDK useChat
Unanswered
Sanderling posted this in #help-forum
SanderlingOP
I am using useChat hook. Got one more thing to ask if anyone knows how to handle return statements from api/chat/route.ts that is not a StreamingTextResponse.
Example, in my api/chat/route.ts I am doing some checking if a file exists in a database
How do I handle in the frontend/client component if the file does not exist and the returns the bad request response? In the client side I want to show a Toast message.
Example, in my api/chat/route.ts I am doing some checking if a file exists in a database
const file = await prisma.file.findFirst();
if (!file) {
return new NextResponse("Bad request", {status: 400})
}
// Do some things here
return new StreamingTextResponse(stream);How do I handle in the frontend/client component if the file does not exist and the returns the bad request response? In the client side I want to show a Toast message.
1 Reply
@Sanderling I am using useChat hook. Got one more thing to ask if anyone knows how to handle return statements from api/chat/route.ts that is not a StreamingTextResponse.
Example, in my api/chat/route.ts I am doing some checking if a file exists in a database
const file = await prisma.file.findFirst();
if (!file) {
return new NextResponse("Bad request", {status: 400})
}
// Do some things here
return new StreamingTextResponse(stream);
How do I handle in the frontend/client component if the file does not exist and the returns the bad request response? In the client side I want to show a Toast message.
try the
onResponse property of useChat https://sdk.vercel.ai/docs/reference/ai-sdk-ui/use-chat#on-response