mongoose 连接到Mongo成功无法读取未定义的属性(阅读“id”)

a0x5cqrl  于 9个月前  发布在  Go
关注(0)|答案(1)|浏览(115)

试图从我创建的mongodb服务器中通过id获取用户详细信息,但id未定义。

//get user details from mongodb server
router.post("/getuser", fetchuser, async (req, res) => {
  try {
    const userId = req.user.id;
    const user = await User.findById(userId).select("-password");
    res.json(user);
  } catch (error) {
    console.error(error.message);
    res.status(500).send("Internal Server Error");
  }
});
module.exports = router;

个字符
我想修复未定义的ID链接到github https://github.com/nirmalyax/notebook-backend上传的项目
[截图] [1]:https://i.stack.imgur.com/PDOAM.png

qcbq4gxm

qcbq4gxm1#

使用body-parser中间件来解析JSON格式的传入请求正文(bodyParser.json())和URL编码的数据(bodyParser.urlencoded())。如果您还没有安装body-parser,请通过npm安装:

npm install body-parser

字符串
然后,像这样在Express应用程序中使用它:

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
const port = 3000;

//  you  can use body-parser middleware here to parse incoming request
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: true })); 


app.listen(port);

相关问题