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

run mtgen got error: RangeError: Maximum call stack size exceeded #76

Closed
aaronyu6 opened this issue Oct 8, 2021 · 14 comments
Closed

run mtgen got error: RangeError: Maximum call stack size exceeded #76

aaronyu6 opened this issue Oct 8, 2021 · 14 comments

Comments

@aaronyu6
Copy link

aaronyu6 commented Oct 8, 2021

tsgen error RangeError: Maximum call stack size exceeded

environment: mongoose 5.13.10

 npx mtgen src/survey/schema/candidateStat.schema.ts -o src/survey/interface/candidateStat.ts
Generating mongoose typescript definitions... !
    RangeError: Maximum call stack size exceeded

Solution: mongoose must run with version 6, version 5.x.x cause the above error. don't know why

@francescov1
Copy link
Owner

Hey @aaronyu6, can you provide an example of the schema in question? Thanks!

@aaronyu6
Copy link
Author

below is the schema file

@aaronyu6
Copy link
Author

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

const PerCatStat = new Schema({
testCatId: {
type: String,
required: true
},
testCatName: {
type: String,
required: true
},
testCatData: {
type: Map,
of: String
}
})

/*cdd is shortform of candidate
stat is shortform of statistic
*/
export const CddStatSchema = new Schema({
schemaVer: {
type: String,
required: true
},
cddSn: {
type: String,
required: true
},
cddName: {
type: String,
required: true
},
cddInfo: {
type: Map,
of: String
},
testDate: Date,
createdDate: Date,
scoreArr: [PerCatStat]
})

export const Candidate = mongoose.model("CddStat", CddStatSchema)

@aaronyu6
Copy link
Author

below is the package-lock.json

@francescov1
Copy link
Owner

Hi @aaronyu6 ,

Im not able to reproduce this. Can you provide a simple repo to repro? Thanks

@francescov1
Copy link
Owner

Havent seen this occur with any other users and unable to repro, going to close off for now. Feel free to re-open if its still occurring.

@iserdmi
Copy link

iserdmi commented Jun 2, 2022

@francescov1 Hello. If you set any attribute of your model to: { type: 'Boolean', default: false } script will throw Maximum call stack size exceeded on parseSchema fn here:

    Object.keys(schemaTree).forEach((key) => {
        const val = schemaTree[key];
        template += parseKey(key, val);

@francescov1
Copy link
Owner

@iserdmi Will put up a fix shortly. As a workaround, you can use { type: Boolean, default: false } (notice that Boolean is not a string, but the global object). Mongoose has many ways to define these types so thanks for pointing this out 🙏

@francescov1
Copy link
Owner

#97

@francescov1
Copy link
Owner

@iserdmi Fix is live in 9.0.6 🚀

@iserdmi
Copy link

iserdmi commented Jun 3, 2022

@francescov1 Thank you very much for this fix!

But now I faced with another problem. Looks like that they are related to each other.

I have model:

const courseLessonSchema = new Schema({
  alias: { type: 'String', required: true },
  title: { type: 'String', required: true },
  desc: { type: 'String' },
  free: { type: 'Boolean', default: false, required: true },
  ready: { type: 'Boolean', default: false, required: true },
  becomeReadyAt: { type: 'Date' },
  creatingDuration: { type: 'Number', default: 7, required: true },
  videoUrl: { type: 'String' },
  content: { type: 'String' },
  index: { type: 'Number', required: true },
  autoFinish: { type: 'Boolean', default: false, required: true },
})

Generator works fine and generate this:

/**
 * Mongoose Schema type
 *
 * Assign this type to new CourseLesson schema instances:
 * ```
 * const CourseLessonSchema: CourseLessonSchema = new mongoose.Schema({ ... })
 * ```
 */
export type CourseLessonSchema = mongoose.Schema<
  CourseLessonDocument,
  CourseLessonModel,
  CourseLessonMethods,
  CourseLessonQueries
>

Then I apply it to my schema:

const courseLessonSchema: CourseLessonSchema = new Schema({
  alias: { type: 'String', required: true },
  ...
})

And now whole object inside new Schema() marked as red with message: "Type instantiation is excessively deep and possibly infinite"

I get this error in all my other schemas.

My mongoose version: 6.3.5

Can you please help me, to figure out?

I am trying to move my project form js to ts. I am already tried typegoose (don't like it). I am already tried to make types for my models by my self (don't like it). I think mongoose-tsgen — the best approach, but I need some help to implement it 🙂

@francescov1
Copy link
Owner

francescov1 commented Jun 9, 2022

I believe this issue is due to an incompatibility with a new Mongoose Typescript change, see here for more details.

We likely wont be addressing this problem, since the next major version of Mongoose contains type inference and will make this package obsolete. In the meantime, you can use --no-populate-overload to get around this, or fix your Mongoose version to 6.3.1, as per this issue.

@iserdmi
Copy link

iserdmi commented Jun 16, 2022

@francescov1 Thanks for recomendation! Tell me please, where I can read more about «next major version of Mongoose contains type inference»?

@francescov1
Copy link
Owner

@iserdmi Here is the PR Automattic/mongoose#11563.

It looks like I will be continuing support for mongoose-tsgen since there's quite a few use cases in which it is still useful. See the comments at the end of that PR for more details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants