Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.85k stars 3.82k forks source link

Saving parent of sub doc causes Maximum Call Stack exceeded. #5538

Closed prakashsanker closed 7 years ago

prakashsanker commented 7 years ago

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

What is the current behavior? In a Jest unit test, I connect to a local mongo db using the provided example for sub docs in the documentation. When I execute save, there is a maximum call stack size exceeded.

/Users/psanker/snaphunt_api/node_modules/mongoose/lib/services/model/applyHooks.js:156
          promise.then(
                  ^
RangeError: Maximum call stack size exceeded
    at Promise.then (<anonymous>)
    at EmbeddedDocument.wrappedPointCut [as save] (/Users/psanker/snaphunt_api/node_modules/mongoose/lib/services/model/applyHooks.js:156:19)
    at EmbeddedDocument._done (/Users/psanker/snaphunt_api/node_modules/hooks-fixed/hooks.js:101:24)
    at _next (/Users/psanker/snaphunt_api/node_modules/hooks-fixed/hooks.js:64:28)
    at fnWrapper (/Users/psanker/snaphunt_api/node_modules/hooks-fixed/hooks.js:186:8)
    at EmbeddedDocument.<anonymous> (/Users/psanker/snaphunt_api/node_modules/mongoose/lib/plugins/sharding.js:16:5)
    at _next (/Users/psanker/snaphunt_api/node_modules/hooks-fixed/hooks.js:62:30)
    at fnWrapper (/Users/psanker/snaphunt_api/node_modules/hooks-fixed/hooks.js:186:8)
    at EmbeddedDocument.<anonymous> (/Users/psanker/snaphunt_api/node_modules/mongoose/lib/plugins/saveSubdocs.js:12:7)
    at _next (/Users/psanker/snaphunt_api/node_modules/hooks-fixed/hooks.js:62:30)

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

I run this in a Jest unit test.
import mongoose from "mongoose";
test("Sanity", async () => {
  const connection = await mongoose.createConnection(
    "mongodb://localhost:27017/test",
    async err => {
      if (err) {
        throw err;
      }
    }
  );

  const childSchema = new mongoose.Schema({
    name: String
  });

  const parentSchema = new mongoose.Schema({
    children: [childSchema]
  });

  const Parent = connection.model("Parent", parentSchema);
  const parent = new Parent({
    children: [{ name: "Matthew" }, { name: "John" }]
  });

  await  parent.save(function(err) {
    console.log(err.message); // #sadpanda
  });
});

What is the expected behavior? For this to properly save.

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

Node version - v8.2.1 Mongo version - 3.4.6 Mongoose version - 4.11.5

sobafuchs commented 7 years ago

closing this is a dupe of https://github.com/Automattic/mongoose/issues/5480