Next.js Discord

Discord Forum

Not able to send a post request in api/profile

Answered
Turkish Angora posted this in #help-forum
Open in Discord
Turkish AngoraOP
I was planning to send the data to backend of nextjs using fetch command and then update that value to the mongodb from there.
But i am getting the error saying method is not allowed.
What should i do here????
Answered by averydelusionalperson
hmm, yeah, it's not like express.js, you need to return NextResponse or Response.

ex: return Response.json({ success: true, message: "New Course created" });
View full answer

41 Replies

hmm, can you share your code & error here?
Turkish AngoraOP
const handleSave = async () => {
try {
fetch("/api/logData", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data22),
});
here this is a demo one to show that it is not working,
share the api?
Turkish AngoraOP
/api/logData

isnt this the api??
i am calling my own website's backend here
I meant the code in the api/logData/route.js/ts
Turkish AngoraOP
// app/api/logData.js
export default function handler(req, res) {
if (req.method === "POST") {
res.status(200).json({ message: "POST request received" });
} else {
res.setHeader("Allow", ["POST"]);
res.status(405).end(Method ${req.method} Not Allowed);
}
}


here my main route file name is appp , it doesn't need to be pages right?
nah, it should be in app dir
Turkish AngoraOP
sir , any changes i need to do in my code?
You can direct export POST function right?

export async function GET(request: Request) {}
 
export async function HEAD(request: Request) {}
 
export async function POST(request: Request) {}
 
export async function PUT(request: Request) {}
 
export async function DELETE(request: Request) {}
 
export async function PATCH(request: Request) {}
 
// If `OPTIONS` is not defined, Next.js will automatically implement `OPTIONS` and  set the appropriate Response `Allow` header depending on the other methods defined in the route handler.
export async function OPTIONS(request: Request) {}
I'm just saying, cause I haven't used like you do.
Turkish AngoraOP
sorry new here,
so all this code will be in my logData?
and what opertaions to do will be written in each of these function?
yeah
Hope this helps
lemme know if the problem is solved
Turkish AngoraOP
still same problem
ohh this time 500 error
can you check logs in your server terminal
Turkish AngoraOP
by server you mean where exactly??
new to this hahah
your terminal where you have ran the command npm run dev
Turkish AngoraOP
Processing POST request
⨯ Error: No response is returned from route handler '.../app/api/logData/route.ts'. Ensure you return a Response or a NextResponse in all branches of your handler.
at .../node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:63974
at async eU.execute (.../node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:53964)
at async eU.handle (.../node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:65062)
at async doRender (.../node_modules/next/dist/server/base-server.js:1315:42)
at async cacheEntry.responseCache.get.routeKind (.../node_modules/next/dist/server/base-server.js:1537:28)
⨯ Detected default export in '.../app/api/profile/route.ts'. Export a named export for each HTTP method instead.
⨯ No HTTP methods exported in '.../app/api/profile/route.ts'. Export a
named export for each HTTP method.
after that i changed code to this format still the issue is there:

import { NextApiResponse, NextApiRequest } from "next";

export async function GET(request: NextApiRequest, response: NextApiResponse) {
console.log("Processing GET request");
// Your code here
response.status(200).json({ message: "GET request processed" });
}
hmm, yeah, it's not like express.js, you need to return NextResponse or Response.

ex: return Response.json({ success: true, message: "New Course created" });
Answer
Turkish AngoraOP
import { NextApiResponse, NextApiRequest } from "next";

export async function GET(request: NextApiRequest, response: NextApiResponse) {
console.log("Processing GET request");
// Your code here
return response.status(200).json({ message: "GET request processed" });
}

i changed to these,

stil error 500 is shown, internal server error
there is no thing such as .status
Turkish AngoraOP
Property 'json' does not exist on type 'void'.ts(2339)

when i write json, this is shown as error
yeah, it's not express.js, just return the json
Turkish AngoraOP
thannk you very very much:aww:
it is working now, can i ask a question here if i again stuck somewhere else?
@Turkish Angora thannk you very very much<:aww:767747875974676520>
Your welcome, if your problem is solved, consider marking the answer as solution.

If you have another question, you may create another post, only one question per post.
Turkish AngoraOP
how to mark the solution?
Original message was deleted
^
right click on the message that helped you solve it, and apps-> mark as solution
🫡
hmm, 1 message is enough
Turkish AngoraOP
ahahah got it