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)})
});