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

Mongoose Schema Array element getter is not called, when it should be #9889

Closed
ahmednaser94 opened this issue Feb 2, 2021 · 0 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@ahmednaser94
Copy link

Do you want to request a feature or report a bug?
bug

What is the current behavior?
When using getters inside schema types, if the property of the schema is an Array, getter of an element is not called when it should be

  // Part of the Schema
    groups: [
      {
        type: Schema.Types.ObjectId,
        ref: 'Group',
        get: function (v: Schema.Types.ObjectId) {
          console.log(`🚀 ~ GET groups ~ line 160`)
          return v ? v.toString() : undefined
        },
        set: function (v: any) {
          console.log(`🚀 ~ SET groups ~ line 164`)
          return v
        },
      }
    ],
    createdBy: {
      type: Schema.Types.ObjectId,
      ref: 'User',
      get: function (v: Schema.Types.ObjectId) {
        console.log(`🚀 ~ GET created by ~ line 174`)
        return v ? v.toString() : undefined
      },
      set: function (v: any) {
        console.log(`🚀 ~ SET created by ~ line 178`)
        return v
      },
    }

Note: just logging to detect which one is fired during operations

If the current behavior is a bug, please provide the steps to reproduce.

https://gist.github.com/ahmednaser94/f0b2e10fb4927da28dbec6d48fdd465a

What is the expected behavior?
It should call get: of groups property and createdBy property of user model

Expected log in terminal

~ GET groups ~ line 160
~ GET created by ~ line 174

What actually happen in 2 different scenarios

  1. When log the inserted document
    it prints
~ GET created by ~ line 174

It didn't call get: of groups property!, but called get: of createdBy

  1. When query this document by its _id using findById method
    it prints
~ SET groups ~ line 164
~ GET created by ~ line 174

It didn't call get: of groups property also!, but called set: instead !!!

why set: of groups called!! it should call get: not set:

it only calls get: inside createdBy, but not inside groups

Also, be noted that, createdBy is one ObjectID, but groups is array of ObjectID

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

  • Mongoose: 5.11.14
  • Mongodb: 4.4.3
  • Node.Js: 14.15.4
{
  "compilerOptions": {
    "target": "ES6",
    "module": "CommonJS",
    "outDir": "dist",
    "removeComments": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "resolveJsonModule": true,
    "moduleResolution": "Node"
  }
}
@vkarpov15 vkarpov15 added this to the 5.11.16 milestone Feb 5, 2021
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Feb 5, 2021
vkarpov15 added a commit that referenced this issue Feb 12, 2021
vkarpov15 added a commit that referenced this issue Feb 12, 2021
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Feb 12, 2021
vkarpov15 added a commit that referenced this issue Feb 12, 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.
Projects
None yet
Development

No branches or pull requests

2 participants