Nextjs returning 500 Internal Server error before getting the POST data
Unanswered
Masai Lion posted this in #help-forum
Masai LionOP
I am working on a project lately which had about 100 users and everything is working fine until 1 - 2 users reported that the app is not working for them. When I checked my vercel logs, I noticed a weird issue happening :-
In the above api route, when the user makes a post request, it should load the json object and log the values, but before it logs the first demo log, it throws a 500 Internal server error :-
[The internal server error](https://i.sstatic.net/ZLJiWBzm.png)
The error doesn't make any sense because either it should have break the app if received the wrong data or throws a new error and explain it, however, the app executes fine and the logged data value is also true
// test.com/api/users/recording/options
export async function POST(request: NextRequest) {
const data = await request.json();
console.log('Hey we received a new data')
console.log(data)
console.log('Succesfully received the data')In the above api route, when the user makes a post request, it should load the json object and log the values, but before it logs the first demo log, it throws a 500 Internal server error :-
[The internal server error](https://i.sstatic.net/ZLJiWBzm.png)
The error doesn't make any sense because either it should have break the app if received the wrong data or throws a new error and explain it, however, the app executes fine and the logged data value is also true
33 Replies
@Double-striped Thick-knee there is a possibility that it's throwing because request wasn't json
Masai LionOP
But why would it log the json after that?
And I don't see any problem in that json
@Masai Lion But why would it log the json after that?
Double-striped Thick-knee
whats the order of log here. top to bottom, or bottom to top
@Double-striped Thick-knee whats the order of log here. top to bottom, or bottom to top
Masai LionOP
bottom to top
@Double-striped Thick-knee whats the order of log here. top to bottom, or bottom to top
Masai LionOP
Here is the full code for the api https://pastecord.com/osezorumur.js
I have been trying to solve this issue since the last 2 days
and its very frustrated
Double-striped Thick-knee
i dont see related logs after this, so maybe it throwed before hitting the console log?
Masai LionOP
Its hard to replicate the issue because its only happening to 1 - 2 users and when I try to do from my end, everything is working fine
maybe, but the point is why?
Why only for that specific user and not for everyone
Double-striped Thick-knee
try wrapping it inside try catch and log it,
const data = await request.json();
const data = await request.json();
@Double-striped Thick-knee try wrapping it inside try catch and log it,
const data = await request.json();
Masai LionOP
you mean the entire function?
Double-striped Thick-knee
just that single line
and ask that user to send request if he can
Masai LionOP
what about the other code?
Double-striped Thick-knee
you can wrap those too if you think they will throw, like line 28.
the db call
@Double-striped Thick-knee you can wrap those too if you think they will throw, like line 28.
Masai LionOP
export async function POST(request: NextRequest) {
try {
const data = await request.json();
}
catch (err: any) {
console.log(err)
}
const data = await request.json();
console.log('Hey we received a new data')
console.log(data)
console.log('Succesfully received the data')
const session = await auth()Kinda like that one?
@Double-striped Thick-knee you can wrap those too if you think they will throw, like line 28.
Masai LionOP
the value "data" has been used all through the code till the bottom
Double-striped Thick-knee
yeah, it will work. tho you can also initialize the variable beforehand and assign it inside
try block@Double-striped Thick-knee yeah, it will work. tho you can also initialize the variable beforehand and assign it inside `try` block
Masai LionOP
let data: any;
try {
data = await request.json();
}
catch (err: any) {
console.log(err)
}
// const data = await request.json();
console.log('Hey we received a new data')
console.log(data)Double-striped Thick-knee
will do the work, just try to recreate that error
Masai LionOP
I have to ask the user again
thats a big of a hassle tho
Double-striped Thick-knee
yeah but that's the only way. 🙂
@Double-striped Thick-knee yeah but that's the only way. 🙂
Masai LionOP
still the same error, it didn't catch it
@Masai Lion still the same error, it didn't catch it
Double-striped Thick-knee
is there any server logs for the error?
@Double-striped Thick-knee is there any server logs for the error?
Masai LionOP
no, same 500 log
@Masai Lion no, same 500 log
Double-striped Thick-knee
try wrapping everything and see if it throws