Everything was going well, after creating GET, POST, PUT, Delete Routes connecting to a database on MongoDB collection -using Node.js and Express- , after more than one successful run, out of nothing SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>)
error shows up linking to some specific lines to files on node_module directory, trying any method on postman afterwards leading to 404 Bad Request with
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected end of JSON input<br> at JSON.parse (<anonymous>)<br> at createStrictSyntaxError (G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\body-parser\lib\types\json.js:169:10)<br> at parse (G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\body-parser\lib\types\json.js:86:15)<br> at G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\body-parser\lib\read.js:128:18<br> at AsyncResource.runInAsyncScope (node:async_hooks:211:14)<br> at invokeCallback (G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\raw-body\index.js:238:16)<br> at done (G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\raw-body\index.js:227:7)<br> at IncomingMessage.onEnd (G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\raw-body\index.js:287:7)<br> at IncomingMessage.emit (node:events:518:28)<br> at endReadableNT (node:internal/streams/readable:1698:12)</pre>
</body>
</html>
The actual cause of error seems vague to me, what I'm really missing here?
Everything was going well, after creating GET, POST, PUT, Delete Routes connecting to a database on MongoDB collection -using Node.js and Express- , after more than one successful run, out of nothing SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>)
error shows up linking to some specific lines to files on node_module directory, trying any method on postman afterwards leading to 404 Bad Request with
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected end of JSON input<br> at JSON.parse (<anonymous>)<br> at createStrictSyntaxError (G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\body-parser\lib\types\json.js:169:10)<br> at parse (G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\body-parser\lib\types\json.js:86:15)<br> at G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\body-parser\lib\read.js:128:18<br> at AsyncResource.runInAsyncScope (node:async_hooks:211:14)<br> at invokeCallback (G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\raw-body\index.js:238:16)<br> at done (G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\raw-body\index.js:227:7)<br> at IncomingMessage.onEnd (G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\raw-body\index.js:287:7)<br> at IncomingMessage.emit (node:events:518:28)<br> at endReadableNT (node:internal/streams/readable:1698:12)</pre>
</body>
</html>
The actual cause of error seems vague to me, what I'm really missing here?
Share Improve this question edited Mar 6 at 17:27 Mahmoud Tarek Mahmoud asked Mar 6 at 17:23 Mahmoud Tarek MahmoudMahmoud Tarek Mahmoud 14 bronze badges2 Answers
Reset to default 0This error typically occurs when there's an issue with the request body parsing or the data sent to the server, especially when creating POST, PUT, and PATCH requests.
To fix this, add the body parsing middleware configuration below to your code to enable nodeJS to parse incoming JSON data correctly.
app.use(express.json());
Adding the code above solved this error when I experienced it.
PS: Next time, add your code or specify which route caused the error so people can fully understand your question.
It looks like the server that is sending the JSON is sending an invalid JSON - could be due to the DB not retrieving any data at all and sending "Null" over the REST endpoint. More context is needed w.r.t to the server code to understand the flow.
Incoming data parsing with express.json() -- may cause the client to crash if the server sends wrong format.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744959296a4603392.html
评论列表(0条)