complete-node-bootcamp icon indicating copy to clipboard operation
complete-node-bootcamp copied to clipboard

Query was already executed: Review.findOne({ _id: new ObjectId(\"62b44ca73c5b3f0162267ea2...",

Open rohit-13 opened this issue 3 years ago • 10 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)"
}

rohit-13 avatar Jun 24 '22 18:06 rohit-13

Attach .clone() after .findOne(). Worked for me

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

muyeenulislam avatar Aug 26 '22 14:08 muyeenulislam

Thanks man, it worked like magic

AgiriTaofeek avatar Oct 06 '22 23:10 AgiriTaofeek

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()

Mae6e avatar Feb 21 '23 17:02 Mae6e

if want to execute a query twice use clone()

this.result = await this.findOne().clone();

amritaB23 avatar Mar 11 '23 04:03 amritaB23

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

govindrevenger avatar Mar 11 '23 11:03 govindrevenger

@govindrevenger

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.

Mae6e avatar Mar 11 '23 16:03 Mae6e

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

manohySr avatar Aug 30 '23 06:08 manohySr

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

Diviner-Beibei avatar Oct 26 '23 14:10 Diviner-Beibei