Open rohit-13 opened 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(); });
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
@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.
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
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!