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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.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
Node.js 获取mongodb中的文档总数_Node.js_Mongodb_Express - Fatal编程技术网

Node.js 获取mongodb中的文档总数

Node.js 获取mongodb中的文档总数,node.js,mongodb,express,Node.js,Mongodb,Express,我是mongodb的新手,正在尝试从集合中获取记录的总数。我正在使用node-express 我的代码- let mongoose = require('mongoose'); let Patient = require('../models/patient'); /* * GET /patient route to retrieve all the patients. */ function getPatients(req, res) { let page = Number(re

我是mongodb的新手,正在尝试从集合中获取记录的总数。我正在使用node-express

我的代码-

let mongoose = require('mongoose');
let Patient = require('../models/patient');

/*
 * GET /patient route to retrieve all the patients.
 */
function getPatients(req, res) {
    let page = Number(req.params.page);
    let pageSize = Number(req.params.pageSize);
    let start = (page-1)*pageSize;

    //Query the DB and if no errors, send all the patients
    let total = Patient.count({});
    //console.log(total);
    let query = Patient.find({});
    query.skip(start);
    query.limit(pageSize);
    query.exec((err, patients) => {
        if(err) res.send(err);
        //If no errors, send them back to the client
        res.json({total:total, page:page, pageSize:pageSize, items:patients});
    });
}
我收到的错误消息是--

TypeError:将循环结构转换为JSON
在JSON.stringify()上

非常感谢您的帮助。提前感谢。

mongoose count是一个异步函数(返回承诺),也可以与回调一起使用

您的错误必须与承诺的
res.json
相关,而不是其结果本身

下面是一个有承诺的解决方案(异步/等待语法): 当然,您也可以对
count
函数使用回调:
尝试
res.json({total:total})
如果效果很好,可以尝试使用
页面
,然后使用
页面大小
,最后是
项目:患者
,这应该可以帮助您找到哪个项目是循环结构。ES6还允许您:
res.json({total,page,pageSize,items:patients})问题在于获取记录的总数,而不是响应。我按照你的建议做了尝试,但似乎不起作用-
let total=Patient.count()
您是否100%确定错误来自上面的表单,而不是匿名
查询.exec((错误,患者)=>{
回调?是的,我非常确定。exec函数为我提供了正确的数据。解释得很好。我已经理解了使用async/Wait的好处。谢谢。
TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
async function getPatients(req, res) {
    let page = Number(req.params.page);
    let pageSize = Number(req.params.pageSize);
    let start = (page-1)*pageSize;

    //Query the DB and if no errors, send all the patients
    let total = await Patient.count({});
    console.log(total); // will log the real count
    let query = Patient.find({});
    query.skip(start);
    query.limit(pageSize);
    query.exec((err, patients) => {
        if(err) res.send(err);
        //If no errors, send them back to the client
        res.json({total:total, page:page, pageSize:pageSize, items:patients});
    });
}
Patient.count({}, (error, count) => {
  if(error) res.send(error);
  // work with total
});