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

How to get the model name/collection name from a document middleware? #13878

Closed
1 task done
JavaScriptBach opened this issue Sep 18, 2023 · 1 comment · Fixed by #13963
Closed
1 task done

How to get the model name/collection name from a document middleware? #13878

JavaScriptBach opened this issue Sep 18, 2023 · 1 comment · Fixed by #13963
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@JavaScriptBach
Copy link
Contributor

Prerequisites

  • I have written a descriptive issue title

Mongoose version

6.12.0

Node.js version

18

MongoDB version

6

Operating system

None

Operating system version (i.e. 20.04, 11.3, 10)

No response

Issue

I'm trying to write a query/document middleware that logs the model name + collection name in certain circumstances.

I looked at https://mongoosejs.com/docs/6.x/docs/api/document.html, and the first problem I ran into is that model/collection name are not documented in the types.

Then I tried writing:

    let modelName = "";
    let collectionName = "";
    if (this instanceof Query) {
      modelName = this.model.modelName;
      collectionName = this.model.collection.name;
    } else if (this instanceof Document) {
      let doc = this;
      // Subdocuments seem to not have model/collection names so keep going up to the parent
      while (isSome(this.$parent()) && doc !== this.$parent()) {
        doc = this.$parent() as Document;
      }
      modelName = (doc.constructor as any).modelName;
      collectionName = doc.collection.name;
    }

But this does not work either in the document case, sometimes I get TypeError: Cannot read properties of undefined (reading 'name') because doc.collection is sometimes undefined. It seems like subdocuments also behave slightly differently than documents in this case.

Surely documents should know which model they came from, so there must be some way to do this. Help would be appreciated; thanks!

@JavaScriptBach JavaScriptBach added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Sep 18, 2023
@vkarpov15
Copy link
Collaborator

Can you provide a repro script that demonstrate the case where doc.collection is undefined? That's unexpected.

Re: types for doc.constructor, we'll fix that.

Also, instead of using a while loop to get the root document, you can just use doc = this.ownerDocument().

@vkarpov15 vkarpov15 added typescript Types or Types-test related issue / Pull Request and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Sep 26, 2023
@vkarpov15 vkarpov15 added this to the 7.5.4 milestone Sep 26, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.5.4, 7.5.5 Oct 4, 2023
vkarpov15 added a commit that referenced this issue Oct 12, 2023
fix(document): allow calling `$model()` with no args for TypeScript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants