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
Schemas in separate project with arrays causes "Schema for array path is from a different copy of the Mongoose module." #13275
Comments
someArrayPath
is from a different copy of the Mongoose module."
Since you've updated since you posted the issue, what error are you seeing, what command are you running? |
Running the level1 and level2 apps in the demo repo should show the problem. I updated the original issues and the demo repo to be more clear. |
I tried to change the way i import mongoose types. Maybe this is more compatible? Pull :) |
The repro script you posted doesn't have a separate package for "shared", "shared" is a separate directory with Mongoose listed as a What's the output of |
All I'm trying to do is reuse some schemas across two or more projects. What would be the right way to do this? |
And by the way. It works as expected when i comment out the line where the exception is thrown. mongoose-schema-error\app\node_modules\mongoose\lib\schema.js:1277 |
Depends on how your project is structured. In the setup in this repo: https://github.com/rndfm/mongoose-schema-error where you have multiple sibling directories, each with their own copy of Mongoose, I'd recommend either:
The problems with multiple copies of the Mongoose modules are 1) incompatibilities between versions, and 2) any |
fix(schema): recursively copy schemas from different modules when calling `new Schema()`
Prerequisites
Mongoose version
7.0.3
Node.js version
18.13.0
MongoDB server version
6.0.5
Typescript version (if applicable)
No response
Description
I have my schemas defined in a separate npm package as they are going to be used be at couple of different services.
When using an array path in the first level i get the following error:
"TypeError: Schema for array path
products
is from a different copy of the Mongoose module. Please make sure you're using the same version of Mongoose everywhere withnpm list mongoose
. If you are still getting this error, please addnew Schema()
around the path: products: new Schema(...)"I tried adding the new Scheme(..) thing. This makes the error message disappear but when i try to new up the model it fails there instead.
BUT. If i comment out the "if and throw" in schema.js: 1276, it works as expected.
The strange thing is. I have another array that works without any errors. But it's on "level 2". Eg. there's another object path before it.
My schema is quite simple:
If i change products to a single(non array) item in the company it works just fine.
My more complex scenario that actually works is a three level deep subDocument.
First level:
Second level:
Third level:
In my shared/common package I am listing mongoose as peerDependency.
All the packages has mongoose version 7.0.3
Steps to Reproduce
I have created a demo repo that shows the problem:
https://github.com/rndfm/mongoose-schema-error
Try and run the apps with:
node level1app.js
node level2app.js
For the level 1 app I get the following error:
For the level 2 app it works just fine.
Expected Behavior
I would expect this to work just the same in any level of the schema tree and not throw any errors.
The text was updated successfully, but these errors were encountered: