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

a0x5cqrl  于 2024-01-08  发布在  Go
关注(0)|答案(1)|浏览(193)

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

  1. //get user details from mongodb server
  2. router.post("/getuser", fetchuser, async (req, res) => {
  3. try {
  4. const userId = req.user.id;
  5. const user = await User.findById(userId).select("-password");
  6. res.json(user);
  7. } catch (error) {
  8. console.error(error.message);
  9. res.status(500).send("Internal Server Error");
  10. }
  11. });
  12. 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安装:

  1. npm install body-parser

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

  1. const express = require('express');
  2. const bodyParser = require('body-parser');
  3. const app = express();
  4. const port = 3000;
  5. // you can use body-parser middleware here to parse incoming request
  6. app.use(bodyParser.json());
  7. app.use(bodyParser.urlencoded({ extended: true }));
  8. app.listen(port);

展开查看全部

相关问题