【问题标题】:MongooseError: Query was already executed: User.countDocuments({})MongooseError:查询已执行:User.countDocuments({})
【发布时间】:2022-01-19 18:43:48
【问题描述】:

(节点:9540)UnhandledPromiseRejectionWarning:MongooseError:查询已执行:User.countDocuments({}) 在 model.Query._wrappedThunk [as _countDocuments] (D:\Acadamic-LANGUAGE-PROJECTS\Angular-Projects\eShop-MEAN STACK\Back-End\node_modules\mongoose\lib\helpers\query\wrapThunk.js:21:19 ) 在 D:\Acadamic-LANGUAGE-PROJECTS\Angular-Projects\eShop-MEAN STACK\Back-End\node_modules\kareem\index.js:370:33 在 processTicksAndRejections (internal/process/task_queues.js:77:11) (使用node --trace-warnings ... 显示警告的创建位置) (节点:9540)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。终止节点 处理未处理的承诺拒绝,使用 CLI 标志--unhandled-rejections=strict(请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。 (拒绝编号:1) (节点:9540)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。

这是我的代码......

router.get(`/get/count`, async (req, res) =>{
const userCount = await User.countDocuments((count) => count)

if(!userCount) {
    res.status(500).json({success: false})
} 
res.send({
    userCount: userCount
});

})

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    看来您正在使用 Mongoose。您似乎在 async-await 和回调之间混用。

    await User.countDocuments((count) => count) 更改为 await User.countDocuments()

    【讨论】:

      【解决方案2】:

      这是因为 countDocuments() 是使用其回调调用(将其结果传递给回调),而另一方面,它也被要求使用 await 命令将其结果传递给 userCount 变量。

      这正是这条错误消息想要表达的意思:嘿,您正在向数据库发送相同的查询两次!同时,由于从 Mongoose v6 开始,您只能运行一次查询 - 即,通过添加 cbk 参数或使用 async-await 块。在这里阅读:https://mongoosejs.com/docs/migrating_to_6.html#duplicate-query-execution

      现在让我们继续解决问题:

      我不完全理解您要这样做:

          const userCount = await User.countDocuments((count) => count)
      

      认为您要做的只是获取文档计数。如果是这样,只需删除 'count => count'。

      router.get(`/get/count`, async (req, res) =>{
      
      const userCount = await User.countDocuments();
      
      if(!userCount) {
          res.status(500).json({success: false})
      } 
      res.send({
          userCount: userCount
      });
      })
      

      如果您要向计数添加过滤器(这是 countDocuments 得到的 - 过滤器;see API here),那么您应该使用键:值对形式,即 {count: count}。

      router.get(`/get/count`, async (req, res) =>{
      
      /* let count; etc. */
      
      const userCount = await User.countDocuments({count: count});
      
      if(!userCount) {
          res.status(500).json({success: false})
      } 
      res.send({
          userCount: userCount
      });
      })
      

      当然你应该在使用 await 时使用适当的 try-catch 块,以便能够处理抛出的错误。

      (自己也遇到了这个问题,研究了一下。)

      【讨论】:

        【解决方案3】:
        module.exports.getUserCount = async(req,res)=>{
        
        const numberOfUser = await User.countDocuments() 
        
        res.send({numberOfUser : numberOfUser});
        
        }
        

        【讨论】:

        • 欢迎来到 Stack Overflow!请在您的答案中添加解释,以便更好地理解代码
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-26
        • 1970-01-01
        • 2011-02-26
        • 2023-03-07
        相关资源
        最近更新 更多