Using Schemas from NPM module in Main App: TypeError: Undefined type at `paths.name`
aarosil opened this issue · 5 comments
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 same mongoose.Schema
as the connection, as well
Trying 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:
TypeError: Undefined type at `paths.name`
Did you try nesting Schemas? You can only nest using refs or arrays.
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!!!!
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 api_client
module not installed, but linked using npm link
. My approach is the only way I could get it working with this situation.
If I understand you correctly, you're saying is that if the module was installed with npm install
, npm wouldn't install it with a separate copy of mongoose under ./node_modules/api_client
, and normal approach should work??
Problem is that I haven't published npm module yet and so can't install
it. What are my options here? Any suggestions to work with this using npm link
, and not having to jump through hoops to re-use same mongoose, as I've done in the linked gist?
There is a way to do it with linked modules too. I made a blog post about how to do that:
http://vadimdev.blogspot.ca/2014/11/npm-link-with-interdependent-modules.html
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 require('mongoose')
around" approach myself for dependency injection (I'd argue its good practice). Hadn't tried with npm link though, because IMO npm link just adds a bunch of complexity to what is a really simple task - you can bypass the entire npm link learning curve by just putting a symlink, for instance ln -s ~/Workspace/mongoose ./node_modules/mongoose
.
Also, it looks like your code works with both npm link and just a plain symlink once you modify app-init.js
a little bit:
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 paths.name
.
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.