MERN - TypeError: Cannot destructure property 'username' of 'req.body' as it is undefined

MERN - TypeError: Cannot destructure property 'username' of 'req.body' as it is undefined

我正在尝试对用户进行身份验证,我首先构建后端并在 Insomnia 上对其进行测试,当我发送请求时出现 TypeError: Cannot destructure property 'username' of 'req.body' as it is undefined. 错误。

我正在使用 passportjs 和 jwt。

这是我的“server.js”文件:https://www.paste.org/110929

这是我的“/users/signin”路线代码:https://www.paste.org/110930

我的 passport.js 文件:https://www.paste.org/110931

如果有任何帮助,我将不胜感激,因为这个问题让我陷入了很长一段时间

您需要将 body-parser 添加到您的 server.js 文件以捕获请求正文

const express = require('express')
const bodyParser = require('body-parser')
 
const app = express()
 
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
 
// parse application/json
app.use(bodyParser.json())

更新

自 express 4.16.0 起,您还可以:

const express = require('express')
 
const app = express()

// parse application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: false }))
     
// parse application/json
app.use(express.json())