jonasschmedtmann / complete-node-bootcamp

Starter files, final projects and FAQ for my Complete Node.js Bootcamp
https://www.udemy.com/nodejs-express-mongodb-bootcamp/?couponCode=GITHUB515
3.13k stars 3.63k forks source link

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

Open rohit-13 opened 2 years ago

rohit-13 commented 2 years ago

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)"
}
muyeenulislam commented 2 years ago

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

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

AgiriTaofeek commented 2 years ago

Thanks man, it worked like magic

Mae6e commented 1 year ago

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

amritaB23 commented 1 year ago

if want to execute a query twice use clone()

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

govindrevenger commented 1 year ago

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

Mae6e commented 1 year ago

@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.

manohySr commented 1 year ago

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

Pan-Beibei commented 1 year ago

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