mongoose: Error when creating with Subdocuments getting RangeError: Maximum call stack size exceeded

Bug

What is the current behavior?

When i try to create a record with a array with objects (subdocuments) i’m getting RangeError: Maximum call stack size exceeded.

  • When i remove the products array from the object to be created, it works.
  • If i use collection.insert instead of create it works
  • Checked the id of the reference model is there

i’m using typescript, webpack and angular universal. I had this working before (almost sure), and i guess this start working after changing the build to work with SSR with angula universal.

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

Create a model like

const OrderSchema = new mongoose.Schema({
  title: {  type: String },
  products: [{
    quantity: { type: Number },
    product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product',unique: false,required: [true,'No product id found']},
    price: { type: Number }
  }]
});

And try to create with a object passing an array of objects:

{\"title\":\"\",\"products\":[{\"product\":\"58f50198dff70a50a147a55d\",\"quantity\":1}]

What is the expected behavior?

Should create the object with the nested attributes.

  • Stacktrace
"RangeError: Maximum call stack size exceeded",
    "    at recursiveSearch (/myproject/web/node_modules/source-map/lib/binary-search.js:24:25)",
    "    at recursiveSearch (/myproject/web/node_modules/source-map/lib/binary-search.js:44:14)",
    "    at recursiveSearch (/myproject/web/node_modules/source-map/lib/binary-search.js:44:14)",
    "    at recursiveSearch (/myproject/web/node_modules/source-map/lib/binary-search.js:59:14)",
    "    at recursiveSearch (/myproject/web/node_modules/source-map/lib/binary-search.js:44:14)",
    "    at recursiveSearch (/myproject/web/node_modules/source-map/lib/binary-search.js:59:14)",
    "    at recursiveSearch (/myproject/web/node_modules/source-map/lib/binary-search.js:59:14)",
    "    at recursiveSearch (/myproject/web/node_modules/source-map/lib/binary-search.js:44:14)",
    "    at recursiveSearch (/myproject/web/node_modules/source-map/lib/binary-search.js:59:14)",
    "    at Object.search (/myproject/web/node_modules/source-map/lib/binary-search.js:94:15)",
    "    at SourceMapConsumer_findMapping [as _findMapping] (/myproject/web/node_modules/source-map/lib/source-m
ap-consumer.js:547:25)",
    "    at SourceMapConsumer_originalPositionFor [as originalPositionFor] (/myproject/web/node_modules/source-m
ap/lib/source-map-consumer.js:604:22)",
    "    at mapSourcePosition (/myproject/web/node_modules/source-map-support/source-map-support.js:195:42)",
    "    at wrapCallSite (/myproject/web/node_modules/source-map-support/source-map-support.js:338:20)",
    "    at /myproject/web/node_modules/source-map-support/source-map-support.js:373:26",
    "    at Array.map (native)",
    "    at Function.prepareStackTrace (/myproject/web/node_modules/source-map-support/source-map-support.js:372
:24)",
    "    at Function.NativeError.prepareStackTrace (/myproject/web/node_modules/zone.js/dist/zone-node.js:1040:3
0)",
    "    at Function.NativeError.prepareStackTrace (/myproject/web/node_modules/zone.js/dist/zone-node.js:1040:3
0)",
    "    at Function.NativeError.prepareStackTrace (/myproject/web/node_modules/zone.js/dist/zone-node.js:1040:3
0)",
    "    at Function.NativeError.prepareStackTrace (/myproject/web/node_modules/zone.js/dist/zone-node.js:1040:3
0)",
    "    at Function.NativeError.prepareStackTrace (/myproject/web/node_modules/zone.js/dist/zone-node.js:1040:3
0)",
    "    at Function.NativeError.prepareStackTrace (/myproject/web/node_modules/zone.js/dist/zone-node.js:1040:3
0)",
    "    at Error.get stack (native)",
    "    at Error.ZoneAwareError (/myproject/web/node_modules/zone.js/dist/zone-node.js:917:59)",
    "    at EmbeddedDocument.wrappedPointCut [as validate] (/myproject/web/dist-server/webpack:/~/mongoose/lib/s
ervices/model/applyHooks.js:109:1)",
    "    at EmbeddedDocument._done (/myproject/web/dist-server/webpack:/~/hooks-fixed/hooks.js:101:1)",
    "    at _asyncsDone (/myproject/web/dist-server/webpack:/~/hooks-fixed/hooks.js:36:1)",
    "    at fnWrapper (/myproject/web/dist-server/webpack:/~/hooks-fixed/hooks.js:186:1)",
    "    at EmbeddedDocument.module.exports.Object.defineProperty.value.fn (/myproject/web/dist-server/webpack:/
~/mongoose/lib/schema.js:252:1)",

Please mention your node.js, mongoose and MongoDB version.

node v6.3.1
 "mongoose": "^4.9.7",
 "mongoose-deep-populate": "^3.0.0",
mongodb version v3.0.7

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 20 (6 by maintainers)

Most upvoted comments

@varunjayaraman - you’re right. I’m getting the same error in hooks.js again. The above-mentioned solution didn’t work now!

I changed jest configuration testEnvironment to node and it started working again (Related: https://github.com/facebook/jest/issues/3045). Not sure what the exact problem is. Temporary workarounds are the only solutions as of now.

@nowke seems to be related, in my case it gives an error on hooks-fixed as well. So doing the creation in 2 steps works (create parent and then after create childs… ) … good to know… will make a test in my side next time i got into that