Skip to content
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

Using Schema.loadClass with a class which inherits from Document will hang up any find operations. #12254

Closed
2 tasks done
WolverinDEV opened this issue Aug 10, 2022 · 0 comments · Fixed by #12391
Closed
2 tasks done
Milestone

Comments

@WolverinDEV
Copy link

WolverinDEV commented Aug 10, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.5.2

Node.js version

v16.13.0

MongoDB server version

docker latest (c8b57c4bf7e3)

Description

When using Schema.loadClass with an class inheriting from Document, loading such entries will stall forever.
A simple fix would be:

import { Document } from "mongoose";
import * as mongoose from "mongoose";

// For details see https://github.com/Automattic/mongoose/issues/12254
mongoose.Schema.prototype.loadClass = function(original: Function) {
    return function(this: mongoose.Schema, model: any, ...args: any[]) {
        if(model === Document) {
            /* Don't load mongoose Document class. */
            return;
        }

        return original.call(this, model, ...args);
    }
}(mongoose.Schema.prototype.loadClass);

Steps to Reproduce

const DownloadJobSchema = new mongoose.Schema();

class B extends Document {}
DownloadJobSchema.loadClass(B);

/* Create at least one entry which could be found. */
this.jobsModel.create({ someKey: "with some data" });

/* Lets find that one entry... */
console.log("Finding one entry");
this.jobsModel.findOne({}).then(() => console.log("You'll never see me!"));

Expected Behavior

findOne finding one entry in the database and return it.

WolverinDEV added a commit to WolverinDEV/mongoose that referenced this issue Aug 10, 2022
@vkarpov15 vkarpov15 added this to the 6.5.5 milestone Aug 24, 2022
vkarpov15 added a commit that referenced this issue Sep 4, 2022
…e custom method is set to Mongoose implementation

Fix #12254
vkarpov15 added a commit that referenced this issue Sep 4, 2022
vkarpov15 added a commit that referenced this issue Sep 7, 2022
fix(model): make `applyHooks()` and `applyMethods()` handle case where custom method is set to Mongoose implementation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants