admin管理员组文章数量:1406052
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.
本文标签:
版权声明:本文标题:node.js - Connecting to MonogDB database suddenly leads to unknown "Unexpected end of JSON input" Error - Stac 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744959295a2634555.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论