Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mongoose在使用路由时返回404,但我在没有路由的情况下得到结果_Javascript_Node.js_Api_Mongoose - Fatal编程技术网

Javascript Mongoose在使用路由时返回404,但我在没有路由的情况下得到结果

Javascript Mongoose在使用路由时返回404,但我在没有路由的情况下得到结果,javascript,node.js,api,mongoose,Javascript,Node.js,Api,Mongoose,当我调用router.get(“”)函数时,我会得到结果。如果我调用router.get(“/:id”)或router.get(“/moscow”),我将从我的API获得404和消息响应。我的API中有以下代码: router.get("", (req, res, next) => { const pageSize = +req.query.pagesize; const currentPage = +req.query.page; let fetchedH

当我调用router.get(“”)函数时,我会得到结果。如果我调用router.get(“/:id”)或router.get(“/moscow”),我将从我的API获得404和消息响应。我的API中有以下代码:

router.get("", (req, res, next) => {
  const pageSize = +req.query.pagesize;
  const currentPage = +req.query.page;
  let fetchedHierarchs;
  const hierarchQuery = Hierarch.find().sort({name: 1, lastName: 1});
  if (pageSize && currentPage) {
      hierarchQuery.skip(pageSize * (currentPage - 1))
      .limit(pageSize);
  }
    
  hierarchQuery
      .then(documents => {
         fetchedHierarchs = documents;
         return Hierarch.countDocuments();
      })
      .then(count => {
         res.status(200).json(fetchedHierarchs);
      });
});

router.get("/moscow", (req, res, next) => {
  Hierarch.find().then(document => {
    if (document) {
      res.status(200).json(document);
    } else {
      res.status(404).json({ message: "Hierarch not found!" });
    }
  });
});

router.get("/:id", (req, res, next) => {
  Hierarch.findById(req.params.id).then(document => {
    if (document) {
      res.status(200).json(document);
    } else {
      res.status(404).json({ message: "Hierarch not found!" });
    }
  });
});
我不知道为什么会这样