【问题标题】:Error while trying to send a Post request. I'm using Nodejs along with MongoDB for the project尝试发送 Post 请求时出错。我在项目中使用 Nodejs 和 MongoDB
【发布时间】:2021-09-02 13:57:11
【问题描述】:

我正在尝试使用 MERN 堆栈构建一个小型社交网站。我已经安装了 Nodejs 以及一些依赖项。当我尝试使用 Postman 发布数据时(在登录页面上 - 必须输入姓名、电子邮件、密码),终端显示此错误,Postman 也显示错误。这里有什么问题?

> SyntaxError: Unexpected token n in JSON at position 1
>     at JSON.parse (<anonymous>)
>     at parse (C:\Users\ASUS\Documents\GitHub\devconnector\node_modules\body-parser\lib\types\json.js:89:19)
>     at C:\Users\ASUS\Documents\GitHub\devconnector\node_modules\body-parser\lib\read.js:121:18
>     at invokeCallback (C:\Users\ASUS\Documents\GitHub\devconnector\node_modules\raw-body\index.js:224:16)
>     at done (C:\Users\ASUS\Documents\GitHub\devconnector\node_modules\raw-body\index.js:213:7)
>     at IncomingMessage.onEnd (C:\Users\ASUS\Documents\GitHub\devconnector\node_modules\raw-body\index.js:273:7)
>     at IncomingMessage.emit (events.js:387:35)
>     at endReadableNT (internal/streams/readable.js:1317:12)
>     at processTicksAndRejections (internal/process/task_queues.js:82:21)

我正在发送以下令牌:

{name : "John Doe", email : "example@gmail.com, password : "somepassword"}

【问题讨论】:

  • Unexpected token n in JSON。您需要向我们展示您发送的 JSON。
  • 已更新。谢谢!
  • 果然,这不是有效的 JSON。 “键”需要用引号引起来,并且您似乎在电子邮件值的末尾缺少一个引号。您可以在线找到大量 JSON 验证器来帮助您。
  • 非常感谢!我是新手,我犯了一个菜鸟错误。现在可以使用了。

标签: node.js mongodb postman mern


【解决方案1】:

您应该将键放在引号中。无论意外令牌如何,此错误通常意味着您尝试发送的内容不是有效的 JSON。试试这个:

{"name": "John Doe", "email": "example@gmail.com", "password": "somepassword"}

【讨论】:

    猜你喜欢
    • 2019-07-21
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多