Zige Private
Zige Private

Reputation: 551

MongooseError: Query was already executed

I updated Mongoose to the latest version (6.0.1) and now I'm getting this error whenever .findOne() is executed:

MongooseError: Query was already executed: Streams.findOne({ i: 6 })
    at model.Query._wrappedThunk [as _findOne] (C:\Users\honza\ZiggerTestMaster\node_modules\mongoose\lib\helpers\query\wrapThunk.js:21:19)
    at C:\Users\honza\ZiggerTestMaster\node_modules\kareem\index.js:370:33
    at processTicksAndRejections (node:internal/process/task_queues:78:11)
    at runNextTicks (node:internal/process/task_queues:65:3)
    at listOnTimeout (node:internal/timers:526:9)
    at processTimers (node:internal/timers:500:7) {
  originalStack: 'Error\n' +
    '    at model.Query._wrappedThunk [as _findOne] (C:\\Users\\honza\\ZiggerTestMaster\\node_modules\\mongoose\\lib\\helpers\\query\\wrapThunk.js:25:28)\n' +
    '    at C:\\Users\\honza\\ZiggerTestMaster\\node_modules\\kareem\\index.js:370:33\n' +
    '    at processTicksAndRejections (node:internal/process/task_queues:78:11)\n' +
    '    at runNextTicks (node:internal/process/task_queues:65:3)\n' +
    '    at listOnTimeout (node:internal/timers:526:9)\n' +
    '    at processTimers (node:internal/timers:500:7)'
}

My code is as follows:

var visitorData = Visitor.find({});
    
app.get("/retrieve", function(req,res){
    visitorData.exec(function (err,data) {
        if (err) {
            throw err;
        }
        res.render("retrieve", { title:"View Visitor Data", records: data});
     });
});

It executes properly the first time I open the route but, whenever I refresh it, it throws the above error. Has been happening since Mongoose got the latest version.

Upvotes: 44

Views: 65546

Answers (13)

Kennedy Mafelo
Kennedy Mafelo

Reputation: 1

I had a similar issue and for me is because I was using the pre find hook asynchronously like this:

review.pre<IReview>(/^find/, async function (next) {
  await this.populate({
    path: "user",
    select: "name photo",
  });
  next();
});

I changed to:

review.pre<IReview>(/^find/, function (next) {
  this.populate({
    path: "user",
    select: "name photo",
  });
  next();
});

Upvotes: 0

rajdeep jadav
rajdeep jadav

Reputation: 1

In my case, I was writing $push two time. Check if are using wrong query.

Hope, this can resolve the issue.

Upvotes: 0

Muhammad Awais
Muhammad Awais

Reputation: 101

schemaName.pre(/^findOneAnd/, async function (next) {
  this.r = await this.findOne().clone();
   console.log(this.r);
  next();
});

use this code ⬇ 

schemaName.pre(/^findOneAnd/, async function (next) {
  this.r = await this.clone().findOne();
   console.log(this.r);
  next();
});


or use npm i mongoose@5

Upvotes: 3

Behzad
Behzad

Reputation: 2200

My mistake was using find() for finding a record like this:

foo.find(a=> a.id === id) //It is wrong!

It should be:

foo.findById(id)

Upvotes: 1

Hrithik Ram
Hrithik Ram

Reputation: 1

The newer version of Mongoose (version 6+) doesn't support executing the same query object twice. So, just downgrade to an older version, like version 5.13.15.

Uninstall the current Mongoose package & then download the older version:

npm uninstall mongoose

npm i mongoose@5

Hope, this can resolve the issue.

Upvotes: -3

Deepu Kumar
Deepu Kumar

Reputation: 57

// Mongoose 6.6.3
// Query.prototype.clone()
// Returns:
// «Query» copy
// Make a copy of this query so you can re-execute it.

// Example:`enter code here`
const q = Book.findOne({ title: 'Casino Royale' });
await q.exec();
await q.exec(); 
// Throws an error because you can't execute a query 
twice

await q.clone().exec(); // Works

Upvotes: 1

Akira Taguchi
Akira Taguchi

Reputation: 141

I'm not sure about the details but mine and my co-workers problems got fixed by downgrading mongoose to 5.13.14

Upvotes: 1

Akshay SM
Akshay SM

Reputation: 161

Don't use the callback within the findOne, instead use .then() , after the Query, like so

 Visitor.find({})
.then(function (err, data) {
        if (!err) {
            res.render("retrieve", { title: "View Visitor Data", records: data });
        } else {
            throw err;
        }
    }

This should most likely work , otherwise use clone() if necessary , as stated by Avis

Upvotes: 14

Prajwal Kulkarni
Prajwal Kulkarni

Reputation: 1695

The error predominantly arises because the same query is executed more than once, the most often cause being unknowingly using promise and callback at the same time, to avoid such mistakes this error is thrown. To get rid of this error, stick to either promise or callback when executing this query method.
Use:

await Model.findOneAndUpdate(query,doc,options)
// (or) regular .then() .catch() syntax
Model.findOneAndUpdate(query)
.then(res=>...)
.catch(err=>...)

(OR)

Model.findOneAndUpdate(query, doc, options, callback)

The same principle applies to similar methods like findByIdAndUpdate.

Upvotes: 5

Iheb Saad
Iheb Saad

Reputation: 367

Mongoose no longer allows executing the same query object twice. If you do, you'll get a Query was already executed error. Executing the same query instance twice is typically indicative of mixing callbacks and promises, but if you need to execute the same query twice, you can call ".clone();" to clone the query and re-execute it.

Upvotes: 1

Nat BK
Nat BK

Reputation: 31

Mongoose v6 does not allow duplicate queries.

Mongoose no longer allows executing the same query object twice. If you do, you'll get a Query was already executed error. Executing the same query instance twice is typically indicative of mixing callbacks and promises, but if you need to execute the same query twice, you can call Query#clone() to clone the query and re-execute it. See gh-7398

Duplicate Query Execution

In my case, I assumed my Apollo Server resolver will wait for the promise to resolve and return the result, but that wasn't the case. So I had to add async/await to the mongoose query.

Upvotes: 3

Gyan Jyoti Das
Gyan Jyoti Das

Reputation: 397

wrap your query within async-await.

const visitorData = async() => { 
  return await Visitor.find({});
}

Upvotes: 15

Avis
Avis

Reputation: 461

Had the same issues, but the release notes on mongoose helped, I chained a .clone() method to the .find() method:

https://mongoosejs.com/docs/migrating_to_6.html#duplicate-query-execution

Mongoose no longer allows executing the same query object twice. If you do, you'll get a Query was already executed error. Executing the same query instance twice is typically indicative of mixing callbacks and promises, but if you need to execute the same query twice, you can call Query#clone() to clone the query and re-execute it.

So all you need to do is to add a .clone() method to the end of the mongoose method that needs to be called concurrently like below (I restructured your code a bit):

app.get("/retrieve", function (req, res) {
    Visitor.find({}, function (err, data) {
        if (!err) {
            res.render("retrieve", { title: "View Visitor Data", records: data });
        } else {
            throw err;
        }
    }).clone().catch(function(err){ console.log(err)})
});

Upvotes: 46

Related Questions