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
Using Schemas from NPM module in Main App: TypeError: Undefined type at paths.name
#2669
Comments
npm by default should only install mongoose once as long as your main module and the sub module have the same version listed in package.json. Your approach isn't necessarily required. |
I have If I understand you correctly, you're saying is that if the module was installed with Problem is that I haven't published npm module yet and so can't |
There is a way to do it with linked modules too. I made a blog post about how to do that: In short, you npm link mongoose in both your main module and your sub module. Then they both wind up using the same mongoose instance. |
So in theory this should work, I've used the "pass result from Also, it looks like your code works with both npm link and just a plain symlink once you modify var mongoose = require('mongoose');
var testClient = require('apiclient');
mongoose.connect('localhost', 'delete-me');
testClient = testClient(mongoose);
testClient.
getItems({name: 'foo'}).
then(function(items) {
console.log(items);
}); I unfortunately can't reproduce your issue with |
I was not clear, the gist was what I got working, and I was asking alternatives. I understand how to do this properly now, thanks to the comments. When installed with npm, everything works as is (as long as package.json versions are compatible). And when working with npm link / manual softlink, just ensure they both point to same instance. Thanks everyone. |
I am creating an npm module that is an API client which provides caching ability for responses
I have read in the
mongoose
documentation that, models are tied to the instance of mongoose which instantializes them, and cannot be shared between connections. However from experimentation I also determined that the Schemas must also be created using the samemongoose.Schema
as the connection, as wellTrying to create the Schema with the mongoose within the npm module, then creating a model out of it with the mongoose instance within the main app (the one that gets connected to the db) resulted in the following error message:
Here is a gist I put together showing what I had to do to get this to work: https://gist.github.com/aarosil/168b5149118247b494a4, basically passed the main app's mongoose through to the npm module which uses that to make it's schemas
Hopefully this all makes sense. Is this the proper way to get done what I'm trying to accomplish? Or is there another easier way to use schemas from a package installed with npm? Is it expected that I can't create a schema with one copy of Mongoose then make a model from it with another?
Thank you!!!!
The text was updated successfully, but these errors were encountered: