SyntaxError: Unexpected token e in JSON at position 3

SyntaxError: Unexpected token e in JSON at position 3

我刚刚安装了 "body-parser": "^1.18.3"(最新),我添加了 express.use(bodyParser.json());。不使用 body parser 我的路线工作正常,但当然 req.body 总是 undefined。我没有在我的应用程序中的任何地方使用 JSON.parse,并且我确定错误来自包本身,除非我在 installing/using 包时错过了一个步骤?

<pre>
    SyntaxError: Unexpected token e in JSON at position 3
    <br> &nbsp; &nbsp;at JSON.parse (&lt;anonymous&gt;)
    <br> &nbsp; &nbsp;at parse (C:\Users\Hafiz\Desktop\NowHiring\backend\node_modules\body-parser\lib\types\json.js:89:19)
    <br> &nbsp; &nbsp;at C:\Users\Hafiz\Desktop\NowHiring\backend\node_modules\body-parser\lib\read.js:121:18
    <br> &nbsp; &nbsp;at invokeCallback (C:\Users\Hafiz\Desktop\NowHiring\backend\node_modules\raw-body\index.js:224:16)
    <br> &nbsp; &nbsp;at done (C:\Users\Hafiz\Desktop\NowHiring\backend\node_modules\raw-body\index.js:213:7)
    <br> &nbsp; &nbsp;at IncomingMessage.onEnd (C:\Users\Hafiz\Desktop\NowHiring\backend\node_modules\raw-body\index.js:273:7)
    <br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:182:13)
    <br> &nbsp; &nbsp;at endReadableNT (_stream_readable.js:1094:12)
    <br> &nbsp; &nbsp;at process._tickCallback (internal/process/next_tick.js:63:19)
</pre>

P.S。其他类似的问题与JSON.parse的错误使用有关,这里不是。

@deceze...认真的???你认为它离题了。

a problem that can no longer be reproduced 可重现...

a simple typographical error 不,这不仅仅是错字问题。仅供参考,Body Parser 是一个包,用于解析您对服务器的所有传入请求。普通人会期望包能够正确处理用户输入,而不是抛出可能导致安全问题的异常。为了争辩,假设这只是一个错字。有时它需要一双额外的眼睛来识别错字。对不起,我的眼睛没有你的机械眼那么完美! smh 不过,它可以帮助可能在 JSONJavascript Object Literals 之间混淆的新手。

错误消息的 SyntaxError: Unexpected token X in JSON at position Y 部分提示传入的 JSON 消息不正确。

请注意 JSON 规范 比普通 Javascript 对象文字更严格,这是我自己经常犯的一个非常常见的错误 :-).