【问题标题】:How to fetch server node js errors?如何获取服务器节点 js 错误?
【发布时间】:2020-01-24 20:04:38
【问题描述】:

我在 Node.js 和 Express 中构建了身份验证应用程序。现在我正在编写后端。 我使用 MongoDB 数据库。

我使用 post 查询来登录带有下一个 JSON 数据的页面。

出现错误 - 电子邮件字段必须使用双引号,但我希望服务器不会崩溃,并以错误回复我

我在下一个代码中使用 body-parser 包

app.use(bodyParser.json()) 
app.use(bodyParser.urlencoded({ extended: true })) 

我有下一个功能:

const bcrypt = require('bcryptjs')
const User = require('../model/User')
const authHelper = require('../helpers/authHelper')
const config = require('../config')

module.exports = async (req, res) => {

  try {
    const {
      email,
      password
    } = req.body

    if(!(email || password)) {
      throw new Error('Enter the properties correctly')
    }

    const user = await User.findOne({
      email
    })
    if (!user) return res.status(400).json({
      message: 'Email or password is wrong'
    })

    const validPass = await bcrypt.compare(password, user.password)
    if (!validPass) return res.status(400).json({
      message: "Email is not found"
    })

    const accessToken = authHelper.generateAccessToken(user._id).token
    const refreshToken = authHelper.generateRefreshToken(user._id).token

    // res.set({
    //   "access-token": accessToken.token,
    //   "refresh-token": refreshToken.token,
    // })

    return res.json({
      "accessToken": accessToken,
      "refreshToken": refreshToken,
    })
  } catch (e) {
    return res.json({
      msg: e.message
    })
  }
}

这里是查询

{
    email: "test@test.com",
    "password": "test"
}

错误信息:

SyntaxError: Unexpected token e in JSON at position 3
at JSON.parse (<anonymous>)
at parse (R:\Projects\jwt-auth\node_modules\body-parser\lib\types\json.js:89:19)
at R:\Projects\jwt-auth\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (R:\Projects\jwt-auth\node_modules\raw-body\index.js:224:16)
at done (R:\Projects\jwt-auth\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (R:\Projects\jwt-auth\node_modules\raw-body\index.js:273:7)
at IncomingMessage.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)

【问题讨论】:

  • 嗨,欢迎来到 Stackoverflow。请花一些时间来编辑您的帖子,并附上一些细节以获得正确的答案。谢谢。
  • 使用 try / catch 块?
  • Remember Express 的全部意义在于拥有链式处理程序,因此您可以添加查找格式错误的 JSON 数据的处理程序。
  • 关于这个问题没有太多信息可以给你一个正确的答案。请编辑您的帖子,至少让我们知道导致您出错的功能是什么

标签: node.js json express


【解决方案1】:

尝试修复电子邮件密钥:

{
    "email": "test@test.com",
    "password": "test"
}

JSON 对象键必须在双引号之间。

【讨论】:

  • 我知道,有错误 - 电子邮件字段必须用双引号引起来,但我希望服务器不会崩溃,并以错误回复我,我在问题中写了这个
  • 你试过 JSON.parse(req.body).then(data => console.log("SUCCESS")).catch(err=> console.log("ERR"))
  • 刚刚试了一下,没有任何反应,但是在我的 server.js 文件中,我使用 body-parser 包和下一个代码 app.use(bodyParser.json()) app.use(bodyParser.urlencoded( { 扩展:真 }))
  • Console.log 在 try{} 之前,就在函数的第一行,那么你就知道错误是在函数中还是在 bodyParse 中间件中
【解决方案2】:

问题可能出在这个结构上:

const {
      email,
      password
    } = req.body

尝试:

const email = req.body.email;
const password = req.body.password;

【讨论】:

  • 这是默认的 ES6 语法,但我试过了,服务器仍然崩溃
猜你喜欢
  • 1970-01-01
  • 2017-12-06
  • 2023-03-10
  • 2021-09-08
  • 1970-01-01
  • 2016-06-12
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
相关资源
最近更新 更多