Query was already executed: Review.findOne({ _id: new ObjectId(\"62b44ca73c5b3f0162267ea2...",
rohit-13 opened this issue · 8 comments
Was implementing the average ratings part, but get this error when updating or deleting the review. Code is almost similar as in the course.
Plaese help!
{
"status": "error",
"error": {
"originalStack": "Error\n at model.Query._wrappedThunk [as _findOne] (/media/rohit/SSD_D1/Project/natours-app/node_modules/mongoose/lib/helpers/query/wrapThunk.js:25:28)\n at /media/rohit/SSD_D1/Project/natours-app/node_modules/kareem/index.js:279:20\n at _next (/media/rohit/SSD_D1/Project/natours-app/node_modules/kareem/index.js:103:16)\n at /media/rohit/SSD_D1/Project/natours-app/node_modules/kareem/index.js:508:38\n at processTicksAndRejections (node:internal/process/task_queues:78:11)",
"statusCode": 500,
"status": "error"
},
"message": "Query was already executed: Review.findOne({ _id: new ObjectId(\"62b44ca73c5b3f0162267ea2...",
"stack": "MongooseError: Query was already executed: Review.findOne({ _id: new ObjectId(\"62b44ca73c5b3f0162267ea2...\n at model.Query._wrappedThunk [as _findOneAndUpdate] (/media/rohit/SSD_D1/Project/natours-app/node_modules/mongoose/lib/helpers/query/wrapThunk.js:21:19)\n at /media/rohit/SSD_D1/Project/natours-app/node_modules/kareem/index.js:279:20\n at _next (/media/rohit/SSD_D1/Project/natours-app/node_modules/kareem/index.js:103:16)\n at /media/rohit/SSD_D1/Project/natours-app/node_modules/kareem/index.js:508:38\n at processTicksAndRejections (node:internal/process/task_queues:78:11)"
}
Attach .clone() after .findOne(). Worked for me
reviewSchema.pre(/^findOneAnd/, async function (next) { this.r = await this.findOne().clone(); // console.log(this.r); next(); });
Thanks man, it worked like magic
Mongoose throws a 'Query was already executed' error when a given query is executed twice.
If you're absolutely sure you want to execute the exact same query twice, you can use clone()
if want to execute a query twice use clone()
this.result = await this.findOne().clone();
can anyone tell me how this this.result = await this.findOne() query runs twice in reviewSchema.pre(/^findOneAnd/, async function (next) { this.r = await this.findOne().clone(); // console.log(this.r); next(); }); this query middleware @Mae6e @amritaB23 @muyeenulislam
findOne is a method used to find and retrieve one document that matches the query criteria from the MongoDB database.
clone() is a method that is used to create a deep copy of the selected document from the MongoDB database.
So, await this.findOne().clone() means that the program will wait until the findOne() method retrieves a document from the database that matches the query criteria, and then clones the document to create a new copy of it.
Attach .clone() after .findOne(). Worked for me
reviewSchema.pre(/^findOneAnd/, async function (next) { this.r = await this.findOne().clone(); // console.log(this.r); next(); });
I suggest using this.clone().findOne() instead
Attach .clone() after .findOne(). Worked for me
reviewSchema.pre(/^findOneAnd/, async function (next) { this.r = await this.findOne().clone(); // console.log(this.r); next(); });
I suggest using this.clone().findOne() instead
Me too