-
Hello,
This works great locally. The behavior works as expected, after clicking create we are redirected to pages/index.js. But on production this behavior breaks somehow. When clicking create, the values from the input fields get pushed to backend and a new trip is created, but we stay on the form page. After a bit of googling and trying, we don't really understand why that is. This error occured after we changed the redirection method to Upon investigating further, the console throws a 405 error when we try to create a trip.
This could possibly come from this:
Take a look at production here. Of course, to make matters a little worse, we got confused in the agile process and already merged the PR thinking it went through QA when it just went through Code Review instead. Many thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey there 👋🏼 The issue actually doesn't lie with the redirection using When I add a trip, it appears in the console as if the If we look at the code in export default async function handler(request, response) {
await connect();
if (request.method === "GET") {
const trips = await Trip.find();
return response.status(200).json(trips);
} else if (request.method === "POST") {
try {
const tripData = request.body;
await Trip.create(tripData);
response.status(201).json({ status: "Trip created" });
} catch (error) {
response.status(400).json({ message: error.message });
}
} else {
return response.status(405).json({ message: "Method not allowed" });
}
} |
Beta Was this translation helpful? Give feedback.
Hey there 👋🏼
The issue actually doesn't lie with the redirection using
router.push
.When I add a trip, it appears in the console as if the
POST
was unsuccessful, and I receive a405
error. In the VS Code terminal, the following error message is displayed: "error Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
." This error occurs when attempting to modify the headers of the HTTP response after sending a response.If we look at the code in
/pages/api/trips
, it seems likeresponse.status(405).json({ message: "Method not allowed" });
is always executed at the end of the handler, regardless of whether any of the two previousif
conditions were met or not. Yo…