简体   繁体   English

如何获得编号。 使用 NodeJs 在 MongoDb 中可用的文档

[英]How to get count of no. of documents available in MongoDb using NodeJs

I am trying to get number of documents available in my collection using NodeJS.我正在尝试使用 NodeJS 获取我的集合中可用的文档数量。 I am getting number successfully in console but it is not sending response and showing below error:我在控制台中成功获取号码,但它没有发送响应并显示以下错误:

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 10 at ServerResponse.writeHead (_http_server.js:246:11) at ServerResponse._implicitHeader (_http_server.js:237:8) at ServerResponse.end (_http_outgoing.js:720:10) at ServerResponse.send (D:\\Backend\\aamku_con_backend\\node_modules\\express\\lib\\response.js:221:10) at D:\\Backend\\aamku_con_backend\\routes\\dealerCount.js:29:50 at executeCallback (D:\\Backend\\aamku_con_backend\\node_modules\\mongodb\\lib\\operations\\execute_operation.js:74:5) at callbackWithRetry (D:\\Backend\\aamku_con_backend\\node_modules\\mongodb\\lib\\operations\\execute_operation.js:122:14) at D:\\Backend\\aamku_con_backend\\node_modules\\mongodb\\lib\\operations\\count_documents.js:36:7 at D:\\Backend\\aamku_con_backend\\node_modules\\mongodb\\lib\\operations\\command_v2.js:90:9 at D:\\Backend\\aamku_con_backend\\node_modules\\mongodb\\lib\\cmap\\connection_pool.js:308:13 { code: 'ERR_HTTP_INVALID_STATUS_CODE' RangeError [ERR_HTTP_INVALID_STATUS_CODE]:无效状态代码:10 at ServerResponse.writeHead (_http_server.js:246:11) at ServerResponse._implicitHeader (_http_server.js:237:8) at ServerResponse.end (_http_outgoing.js:720:10) at ServerResponse.send (D:\\Backend\\aamku_con_backend\\node_modules\\express\\lib\\response.js:221:10) 在 D:\\Backend\\aamku_con_backend\\routes\\dealerCount.js:29:50 在 executeCallback (D:\\Backend\\ aamku_con_backend\\node_modules\\mongodb\\lib\\operations\\execute_operation.js:74:5) 在 callbackWithRetry (D:\\Backend\\aamku_con_backend\\node_modules\\mongodb\\lib\\operations\\execute_operation.js:122:14) 在 D:\\Backend\\ aamku_con_backend\\node_modules\\mongodb\\lib\\operations\\count_documents.js:36:7 在 D:\\Backend\\aamku_con_backend\\node_modules\\mongodb\\lib\\operations\\command_v2.js:90:9 在 D:\\Backend\\aamku_con_backend\\node_modules\\ mongodb\\lib\\cmap\\connection_pool.js:308:13 { 代码:'ERR_HTTP_INVALID_STATUS_CODE'

Below is my code:下面是我的代码:

router.get("/dealerCount", (req, res) => {        
  MongoClient.connect(dburl,{ useNewUrlParser: true, useUnifiedTopology: true },
    (err, client) => {
      if (err) {
        console.log("Error", err);
      } else {
        const coll = client.db("Mydb").collection("Dealers");

        coll.countDocuments(function(err, resp) {
          if (err) {
            console.log("Error", err);
          } else {
            console.log(resp);
            res.send(resp);
          }
        });
      }
    }
  );
});

Someone please let me know how can I get total count in response.有人请让我知道我怎样才能得到总数作为回应。 Any help would be appreciated.任何帮助,将不胜感激。

The problem is res.send(resp);问题是res.send(resp); line.线。 It is making express think that you are sending a integer which is not allowed.它使 express 认为您正在发送一个不允许的整数。 Replace that with this用这个替换它

res.send({"value": resp});

OR

res.send(resp.toString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算不。 Node.Js在MongoDB集合中收集文档? - How to count no. of documents in MongoDB collection using Node.Js ? 如何同时查找文档并使用 mongoose 从 mongodb 获取它们的计数 - How simultaneously find documents and get their count from mongodb using mongoose 如何使用nodeJs获取mongodb中相同值数据的计数 - How to get the count for same value data in mongodb using nodeJs 如何通过在nodejs中使用多个字段的搜索操作使用mongoose获取与mongodb中的字段匹配的所有文档 - how to get all the documents that are matching the fields in mongodb using mongoose by using search operation with multiple fields in nodejs 使用 nodejs 和 mongodb 从集合中获取多个文档 - Get multiple documents from collection using nodejs and mongodb MongoDB 使用 Node.js 获取集合中的文档数(计数) - MongoDB get the number of documents (count) in a collection using Node.js 使用限制时使用 MongoDB 聚合获取总文档数 - Get a count of total documents with MongoDB aggregation when using limit 如何使用 nodejs 在我的控制台中打印 Mongodb 的更新文档? - How to print updated documents of Mongodb in my console using nodejs? 如何计算 mongodb/express 中的文档数 - How to count documents in mongodb/express 使用nodejs在MongoDB中获取数据库的所有文档 - Fetch all documents of a database in MongoDB using nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM