首页 > 解决方案 > TypeError: 将循环结构转换为 JSON\n

问题描述

我有一个端点,只返回一个模式的 countDocuments。我想返回一个这样的 JSON 对象

{count:3500}

但我收到了这个错误:

Converting circular structure to JSON\n    --> starting at object with constructor 'NativeTopology'\n    |     property 's' -> object with constructor 'Object'\n    |     property 'sessionPool' -> object with constructor 'ServerSessionPool'\n    --- property 'topology' closes the circle"}}

那是我的代码:

路由.js

router.get('/', asyncWrapper(async (req, res) => {  
    const census = await censusService.getCensus();
    return res.send(census);
  }));

人口普查服务.js

async function getCensus(){
        return await censRepository.getCensus();
    }

存储库.js

async getCensus(){
   
    const { Cens: censSchema } = this.getSchemas();
    const countCensus = censSchema.countDocuments({}, function(err, count) {
      if (err) { return handleError(err) } 
    })
    
    return {
      count: countCensus
    }

  }

标签: javascriptmongodbexpressmongoose

解决方案


推荐阅读