MongoDB过滤问题



我不确定我的代码有什么问题,我在文档中看过,一切似乎都是正确的。我看了很多帖子,尝试了很多解决方案。

代码:

router.get('/rank/:rank', async (req, res) => {
const users = await loadUserCollection()
const query = users.find({rank: req.params.rank}, {projection: {email: 0, password: 0}})
res.json(query)
})
async function loadUserCollection() {
const client = await mongodb.MongoClient.connect(mongoUri, {useNewUrlParser: true})
return client.db('myFirstDatabase').collection('users')
}

错误:

[0] (node:39544) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
[0]     --> starting at object with constructor 'ReplSet'
[0]     |     property 's' -> object with constructor 'Object'
[0]     |     property 'coreTopology' -> object with constructor 'ReplSet'
[0]     |     ...
[0]     |     property 's' -> object with constructor 'Object'
[0]     --- property 'topology' closes the circle
[0]     at JSON.stringify (<anonymous>)
[0]     at stringify (C:______________node_modulesexpresslibresponse.js:1123:12)
[0]     at ServerResponse.json (C:_________node_modulesexpresslibresponse.js:260:14)      
[0]     at C:____________routesapiusers.js:19:9
[0]     at processTicksAndRejections (internal/process/task_queues.js:95:5)

Try const query = await users。找到({排名:req.params。Rank},{投影:{email: 0,密码:0}})

最新更新