邮递员为Mongoose查询返回“null”

问题描述 投票:0回答:1

我有一个Mongoose User模型,其中包含username字段。我创建了一个用户名为bob97555x005的用户

users路由器中,我有以下路线:

router.get("/check", (req, res, next) => {

  const query = String(req.body.username).toLowerCase();

  return User.findOne({ username: query })
    .then(users => res.json(users))
    .catch(err => {
      console.log("no user found!");
    });
});

在Postman,我正在用这条路线:

http://localhost:5000/api/users/check?username=bob97555x005

这将返回null。预期的行为是返回数据库中的用户json。我为什么得到null

附:路由器文件设置本身没有任何问题,因为我正在使用它来创建用户。

express mongoose postman mern
1个回答
1
投票

由于您使用Http get方法,您应该将传递的参数检索为req.query.username

const query = String(req.query.username).toLowerCase();

编辑

使用Http Post方法时使用req.body。

© www.soinside.com 2019 - 2024. All rights reserved.