Issue
Right now am using different ways to return API HTTP status in my NextJS-13 but nothing seems to be working for me.
Note: I am using Typescript in my project.
Here is my code with static 200 API response and am sending API status in body:
type postProps = {
title: string;
content?: string;
published: boolean;
};
export async function POST(request: Request) {
const post: postProps = await request.json();
if (!post.title) {
return NextResponse.json({
status: 400,
message: "Please enter title",
});
}
}
I have tried
import type { NextApiRequest, NextApiResponse } from "next";
export async function POST(response: NextApiResponse, request: NextApiRequest ) {
const post: postProps = await request.body;
if (!post.title) {
return response.status(400).json({
message: "Please enter title"
})
}
}
But it give me TypeError: res.status is not a function
I also have tried
import type { NextApiRequest, NextApiResponse } from "next";
export async function POST(response: Response, request: Request) {
const post: postProps = await request.json();
if (!post.title) {
return response.status(400).json({
message: "Please enter title"
})
}
}
But it give me the following error: This expression is not callable. Type 'Number' has no call signatures.
Solution
import { NextResponse } from "next/server";
NextResponse.json({
message: "Please enter title"
}, {
status: 400,
})
Answered By - brainli
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.