Skip to content

Model.prototype.save() no longer accepts a callback #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Able1991 opened this issue Feb 13, 2024 · 3 comments · May be fixed by #163
Open

Model.prototype.save() no longer accepts a callback #153

Able1991 opened this issue Feb 13, 2024 · 3 comments · May be fixed by #163

Comments

@Able1991
Copy link

MongooseError: Model.prototype.save() no longer accepts a callback"
    at model.save (/Users/User/Workspace/app/node_modules/mongoose/lib/model.js:517:11)"
    at model.schema.methods.delete (/Users/User/Workspace/app/node_modules/mongoose-delete/index.js:248:25)"
    at /Users/User/Workspace/app/node_modules/kareem/index.js:387:18"
    at /Users/User/Workspace/app/node_modules/kareem/index.js:51:7"
    at processTicksAndRejections (node:internal/process/task_queues:78:11)
@newbies779
Copy link

I face the same problem, but only with schema.pre('delete', xxxx)

@tomLadder
Copy link

Having the same issue

@Able1991
Copy link
Author

I temporarily solved the problem with another plugin

export default function mongooseDeleteFix(schema, options) {
  schema.methods.restore = async function (callback) {
    this.deleted = false;
    this.deletedAt = undefined;
    this.deletedBy = undefined;

    if (options.validateBeforeRestore === false) {
      try {
        await this.save({ validateBeforeSave: false });
      } catch (err) {
        return callback(err);
      }

      return this;
    }

    try {
      await this.save();
    } catch (err) {
      return callback(err);
    }

    return this;
  };

  schema.methods.delete = async function (deletedBy, cb) {
    if (typeof deletedBy === "function") {
      cb = deletedBy;
      deletedBy = null;
    }

    this.deleted = true;

    if (schema.path("deletedAt")) {
      this.deletedAt = new Date();
    }

    if (schema.path("deletedBy")) {
      this.deletedBy = deletedBy;
    }

    if (options.validateBeforeDelete === false) {
      try {
        await this.save({ validateBeforeSave: false });
      } catch (err) {
        return cb(err);
      }

      return this;
    }

    try {
      await this.save();
    } catch (err) {
      return cb(err);
    }

    return this;
  };
}

@tomLadder tomLadder linked a pull request Mar 25, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants