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

Static variables declared on advanced schemas are not loaded into the model #10206

Closed
meircarlos opened this issue May 5, 2021 · 5 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Milestone

Comments

@meircarlos
Copy link

When declaring a static variable on a class loaded with loadClass into the schema, the static variable does not get loaded into the model.

In order for it to work, you need to explicitly declare the static variable through the classic function property schema.statics approach.

In order to reproduce:

  1. Create a model through advanced schemas
  2. Add a static variable, example: static TEST = true;
  3. Try accessing the static variable from the model (it will not exist)

Mongoose v5.9.2

@IslandRhythms IslandRhythms added needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels May 5, 2021
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented May 5, 2021

static TEST = true;

This isn't standard javascript so we need to know more about your set up if we are going to attempt to repro this issue. Alternatively, you can provide a script with notes about your setup that demonstrates this problem.

@meircarlos
Copy link
Author

Here is the code to reproduce this issue:

const mongoose = require("mongoose");

const schema = new mongoose.Schema({ firstName: String, lastName: String });

class PersonClass {
  get fullName() {
    return `${this.firstName} ${this.lastName}`;
  }

  set fullName(v) {
    const firstSpace = v.indexOf(" ");
    this.firstName = v.split(" ")[0];
    this.lastName = firstSpace === -1 ? "" : v.substr(firstSpace + 1);
  }

  getFullName() {
    return `${this.firstName} ${this.lastName}`;
  }

  static findByFullName(name) {
    const firstSpace = name.indexOf(" ");
    const firstName = name.split(" ")[0];
    const lastName = firstSpace === -1 ? "" : name.substr(firstSpace + 1);
    return this.findOne({ firstName, lastName });
  }

  // Does not work
  static TEST = true;
}

schema.loadClass(PersonClass);

// Uncomment this line for it to work
// schema.statics.TEST = true;

var Person = mongoose.model("Person", schema);

console.log(Person.TEST); // undefined

@IslandRhythms
Copy link
Collaborator

static Test = true

This applies to the person class so

console.log(Person.TEST);

will return undefined but

console.log(PersonClass.TEST) will return true

@IslandRhythms IslandRhythms added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels May 5, 2021
@meircarlos
Copy link
Author

Sure, but the static method findByFullName also belongs to the PersonClass and it's accessible from the Person model, why would this behavior be avoided for static variables when it works correctly for static methods?

Otherwise the workaround I'm currently doing is duplicating the static variable and adding it to the schema statics, as you can see on the commented lines of code

@IslandRhythms IslandRhythms self-assigned this May 6, 2021
@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels May 6, 2021
@IslandRhythms
Copy link
Collaborator

We'll be implementing this soon

@IslandRhythms IslandRhythms removed their assignment May 6, 2021
@vkarpov15 vkarpov15 added this to the 5.12.9 milestone May 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Projects
None yet
Development

No branches or pull requests

3 participants