francescov1 / mongoose-tsgen

A plug-n-play Typescript generator for Mongoose.
105 stars 25 forks source link

TypeError: Cannot read property 'comments' of undefined #78

Closed joshgachnang closed 2 years ago

joshgachnang commented 2 years ago

I just finished upgrading a project to mongoose@6.1.8 and mongoose-tsgen@8.4.8 and I couldn't generate types any more. After a lot of digging (we also have a few fields named comments that threw me), I found this line: https://github.com/francescov1/mongoose-tsgen/blob/4b7b256e08598e3f1ec3c22856836a3aa5350bc9/src/helpers/tsReader.ts#L73

It looks like sometimes modelName is undefined here (3 times out of about 50 models), causing the above issue. I tried to make a repro or even pin down which of our models was causing the issue, but wasn't able to, sorry! I do have a ton of new tsreader: No schema found in file. If a schema exists & is exported, it will still be typed but will use generic types for methods, statics, queries & virtuals warnings while generating types, which is maybe related?

francescov1 commented 2 years ago

Thanks for reporting, sorry to hear of the issues! I'll look into this ASAP and get back to you with more info.

joshgachnang commented 2 years ago

No worries!

joshgachnang commented 2 years ago

FWIW this is the comment that's causing an issue. I guess because it expects that to be a JS doc? Or maybe because it's in the middle of a schema declaration?

const AudienceSchema = new mongoose.Schema({

  .....

  /**
   * DEPRECATED FIELDS: These are now per group. But they will remain around to support old clients.
   */
  // Whether posts should be tagged with their location.
  locationEnabled: Boolean,

  .....
francescov1 commented 2 years ago

Awesome that's super helpful. Will look to get a fix up ASAP, likely by end of weekend!

francescov1 commented 2 years ago

@joshgachnang do you mind providing the options your using with mtgen and the way you export your schema? I think its related to the export here. A simple repro would be super useful if its not too much of a hassle!

joshgachnang commented 2 years ago

For options, I'm just doing mtgen -o ./modelInterfaces.ts models/. This is a stripped down version of how our model files look:

import mongoose from "mongoose";
import {AudienceDocument, AudienceModel} from "../modelInterfaces";

const AudienceSchema = new mongoose.Schema({
    // A field
    field1: Boolean,
    /**
     * DEPRECATED FIELDS: These are now per group. But they will remain around to support old clients.
     */
    // Whether posts should be tagged with their location.
    locationEnabled: Boolean,
    field2: Boolean,
});

export const Audience = mongoose.model<AudienceDocument, AudienceModel>(
    'Audience',
    AudienceSchema
);

I tried to make repro (for this and the other issue I reported) just now but of course on a blank project, even with all the same dependencies and everything...it works just fine! I'll keep digging on my side to see if I can figure it out (I'm also leaving for vacation, so it might be another week or two).

mike-van commented 2 years ago

In my case, it was JS doc declaration in the nested schema.

const schema: OrderSchema = new mongoose.Schema({
    index: {
        type: Number,
        default: 0,
        unique: true,
    },
    created_at: {
        type: Date,
        default: Date.now,
    },
    /** JS Doc here */   ========= WORKS!!
    nested: [nestedSchema]
})

const nestedSchema = new mongoose.Schema({
    /** JS Doc here */   ========= DOESN'T WORK!!
    title: {
        type: String,
        required: true,
    }
})
francescov1 commented 2 years ago

Thanks for the example @Mike-Van, are you still seeing this occur in the latest version? If you could provide a simple repro project I can fix this right away 😊

mike-van commented 2 years ago

Hey @francescov1 thanks for replying, yea im still seeing that in the latest version. weirdly now, someitme it also throw same errors sometime if there's JSDocs in the parent schema as well. I've made a repro project here: https://github.com/Mike-Van/test-api

francescov1 commented 2 years ago

Thanks for the repo @Mike-Van, I'll investigate and get back to you before end of weekend!

francescov1 commented 2 years ago

@Mike-Van I found the issue, there was a dangling comma in your model initialization which screwed up our regex match. Fixed here: https://github.com/francescov1/mongoose-tsgen/pull/90