Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongooseError: Query was already executed: User.countDocuments({})

(node:9540) UnhandledPromiseRejectionWarning: MongooseError: Query was already executed: User.countDocuments({}) at 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) at D:\Acadamic-LANGUAGE-PROJECTS\Angular-Projects\eShop-MEAN STACK\Back-End\node_modules\kareem\index.js:370:33 at processTicksAndRejections (internal/process/task_queues.js:77:11) (Use node --trace-warnings ... to show where the warning was created) (node:9540) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:9540) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

this is my Code......

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
});

})

like image 430
Sheik Mohideen.M Avatar asked Nov 15 '25 03:11

Sheik Mohideen.M


2 Answers

It seems that you are using Mongoose. It seems you are mixing between async-await and callbacks.

Change await User.countDocuments((count) => count) to await User.countDocuments()

like image 197
Raz Avatar answered Nov 17 '25 18:11

Raz


This is because countDocuments() is called using its callback (which passes its result to the callback), while on the other hand, it is also asked to pass its result to the userCount variable using the await command.

This is exactly what this error message is trying to say: hey, you're sending the same query to the database twice ! While, since since v6 of Mongoose, you can only get run query once - ie, either by adding the cbk argument, or using async-await block. Read about it here: https://mongoosejs.com/docs/migrating_to_6.html#duplicate-query-execution

Now let's move ahead to fixing the problem:

I don't completely understand what you're trying to do this in line:

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

I think what you're trying to do is just get the document count. If so, simply drop '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
});
})

If you were to add a filter to the count (which is what the countDocuments gets - a filter; see API here), then you should use the key:value pair form, ie {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
});
})

Of course you should use a proper try-catch block when using await, to be able to handle the error if thrown.

(Just encountered this problem myself and made some research into it.)

like image 31
tbger99 Avatar answered Nov 17 '25 19:11

tbger99



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!