node.js - Connecting to MonogDB database suddenly leads to unknown "Unexpected end of JSON input" Error - Stac

Everything was going well, after creating GET, POST, PUT, Delete Routes connecting to a database on Mon

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> &nbsp; &nbsp;at JSON.parse (&lt;anonymous&gt;)<br> &nbsp; &nbsp;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> &nbsp; &nbsp;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> &nbsp; &nbsp;at G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\body-parser\lib\read.js:128:18<br> &nbsp; &nbsp;at AsyncResource.runInAsyncScope (node:async_hooks:211:14)<br> &nbsp; &nbsp;at invokeCallback (G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\raw-body\index.js:238:16)<br> &nbsp; &nbsp;at done (G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\raw-body\index.js:227:7)<br> &nbsp; &nbsp;at IncomingMessage.onEnd (G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\raw-body\index.js:287:7)<br> &nbsp; &nbsp;at IncomingMessage.emit (node:events:518:28)<br> &nbsp; &nbsp;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> &nbsp; &nbsp;at JSON.parse (&lt;anonymous&gt;)<br> &nbsp; &nbsp;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> &nbsp; &nbsp;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> &nbsp; &nbsp;at G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\body-parser\lib\read.js:128:18<br> &nbsp; &nbsp;at AsyncResource.runInAsyncScope (node:async_hooks:211:14)<br> &nbsp; &nbsp;at invokeCallback (G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\raw-body\index.js:238:16)<br> &nbsp; &nbsp;at done (G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\raw-body\index.js:227:7)<br> &nbsp; &nbsp;at IncomingMessage.onEnd (G:\iti\9-Month Program\Open Source Apllication Development\Coding\Node.Js\node_modules\raw-body\index.js:287:7)<br> &nbsp; &nbsp;at IncomingMessage.emit (node:events:518:28)<br> &nbsp; &nbsp;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 badges
Add a comment  | 

2 Answers 2

Reset to default 0

This 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信