Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.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 paginate-始终会丢失一份文档_Javascript_Node.js_Express_Mongoose - Fatal编程技术网

Javascript Mongoose paginate-始终会丢失一份文档

Javascript Mongoose paginate-始终会丢失一份文档,javascript,node.js,express,mongoose,Javascript,Node.js,Express,Mongoose,我有6份文件的db,以下是我的路线: router.get('', async (req, res) => { const search = req.query.search !=null ? req.query.search : ""; const page = req.query.page !=null ? req.query.page : 1; const limit = req.query.limit !=null ? req.query.limit : 4; try {

我有6份文件的db,以下是我的路线:

router.get('', async (req, res) => {
const search = req.query.search !=null ? req.query.search : "";
const page = req.query.page !=null ? req.query.page : 1;
const limit = req.query.limit !=null ? req.query.limit : 4;
try {
    User.paginate({fullname: {$regex: search, $options: '-i'}}, {page: page, limit: limit, customLabels: myCustomLabels, select:'-email -password'}, async (err, result) => {
            res.json(result)
    }
    );
}
catch(e){
    res.status(500).json({ message: 'somthing went wrong, try again...  ERROR :' + e });
}
})

在第一个响应(1页,4个项目)中,我可以看到文档总数为6,但当我发送第二个请求(第2页)时,它表示文档总数为5,并且在数组中只发送了一个文档。 如果我将每页项目数更改为5秒,请求也将发送空数组。 我不明白为什么一份文件总是丢失

UPD:当我删除{fullname:{$regex:search,$options:'-i'}时,它的工作是正确的


我不知道为什么它失去了regex选项。谢谢

首先,如果您正在使用库,或者只是将require/import部分添加到示例代码中,那么最好说明一下,因为mongoose上不存在
Model.paginate()

您还将async/await与传统回调语法混合使用。您可以使您的代码更清晰、更简单

我认为您的一个文档可能没有全名字段。这就是为什么当你搜索时它不会出现的原因。无论如何,请尝试一下代码:

router.get('', async (req, res) => {
  const { search = "", page = 1, limit = 4 } = req.query;
  const paginate = {};
  if (search) {
    paginate.fullname = {
      $regex: search, 
      $options: '-i'
    }
  };
  try {
    const result = await User.paginate(paginate, {
      page, 
      limit, 
      customLabels: myCustomLabels, 
      select:'-email -password'
    });
    return res.json(result);
  } catch(e) {
    res.status(500).json({ message: 'somthing went wrong, try again...  ERROR :' + e });
  }