# Fix Mongoose Callback Error in Version 7 and Later

> Replace deprecated callbacks with async/await or promises when Mongoose 7+ throws 'no longer accepts a callback', with migration guide links

**URL:** https://sentry.io/answers/mongoose-stopped-accepting-callbacks-for-some-of-its-functions/

---

## The Problem

When using [Mongoose](https://mongoosejs.com/), which is a Node.js library for [MongoDB](https://www.mongodb.com/) 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()](https://mongoosejs.com/docs/api/model.html#Model.prototype.save()) method could be called with an optional callback function as an argument to handle an error:

```js
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.

## The Solution

You can avoid using callbacks by using the `async`/`await` syntax in an asynchronous function:

```js
try {
  await product.save();
} catch (err) {
  // ...
}
```

[Using `async`/`await` with Mongoose](https://mongoosejs.com/docs/async-await.html) 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](https://masteringjs.io/tutorials/tools/callback-to-async-await), built by the maintainer of the Mongoose library.

You can also have a [top-level `await`](https://nodejs.org/api/esm.html#top-level-await) in ES modules from Node.js v14.8.0.

You can use promises as an alternative to `async`/`await`:

```js
product.save()
  .then(function() {
    // Product saved successfully
  })
  .catch(function(err) {
    // Handle the error
  });
```

### Note

You can find a full list of the methods that no longer accept callbacks in the [Mongoose docs](https://mongoosejs.com/docs/migrating_to_7.html#dropped-callback-support), including the following methods:

- `Connection.prototype.startSession`
- `Model.find`
- `Model.create`
- `Model.bulkWrite`
- `Model.prototype.save`
- `Query.prototype.findOneAndUpdate`

### Best Practice

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:

- [Migrating from 7.x to 8.x](https://mongoosejs.com/docs/migrating_to_8.html)
- [Migrating from 6.x to 7.x](https://mongoosejs.com/docs/migrating_to_7.html)
- [Migrating from 5.x to 6.x](https://mongoosejs.com/docs/migrating_to_6.html)

---

*Source: [sentry.io/answers/mongoose-stopped-accepting-callbacks-for-some-of-its-functions/](https://sentry.io/answers/mongoose-stopped-accepting-callbacks-for-some-of-its-functions/)*
