Matthew C.
—When using Mongoose, which is a Node.js library for MongoDB object modeling, you may encounter an error similar to the following:
MongooseError: Model.prototype.save() no longer accepts a callback
This error occurs when passing a callback as an argument to certain Mongoose methods in Mongoose version 7+.
For example, before version 7, the Model.save() method could be called with an optional callback function as an argument to handle an error:
product.save(function(err) { // ... });
But from version 7, using a callback as an argument in the save()
method returns undefined
,
because the method can no longer accept callback arguments and instead returns a promise.
You can avoid using callbacks by using the async
/await
syntax in an asynchronous function:
try { await product.save(); } catch (err) { // ... }
Using async
/await
with Mongoose improves code readability when executing multiple async Mongoose methods in sequence. This prevents callback hell, which is when you have multiple levels of callbacks within callbacks.
To convert your Mongoose methods to use async
/await
syntax, you can use the free tool, Convert Callbacks to Async/Await with OpenAI, built by the maintainer of the Mongoose library.
Async
/await
is supported in Node.js version 8+. You can also have a top-level await
from Node.js v14.8.0.
If your Node.js version doesn’t support async
functions, use a promise instead:
product.save() .then(function() { // Product saved successfully }) .catch(function(err) { // Handle the error });
You can find a full list of the methods that no longer accept callbacks in the Mongoose docs, including the following methods:
Connection.prototype.startSession
Model.find
Model.create
Model.bulkWrite
Model.prototype.save
Query.prototype.findOneAndUpdate
If you are migrating to a new Mongoose version, it’s best to migrate incrementally from version to version, as each upgrade has several backward-breaking changes. Here are the migration guides:
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.