【问题标题】:Postman returning "null" for Mongoose query邮递员为 Mongoose 查询返回“null”
【发布时间】:2019-02-07 23:00:38
【问题描述】:

我有一个 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】:

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

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

    编辑

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

    【讨论】:

    • 谢谢你的回答,但那是一个错字,很抱歉!代码仍然无法工作,这不是问题。
    猜你喜欢
    • 2020-01-29
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 2019-09-11
    • 2019-11-03
    • 2020-05-19
    • 2019-08-29
    • 2022-08-02
    相关资源
    最近更新 更多