Skip to content
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

Closed
aarosil opened this issue Feb 11, 2015 · 5 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@aarosil
Copy link

aarosil commented Feb 11, 2015

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!!!!

@yads
Copy link

yads commented Feb 11, 2015

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.

@aarosil
Copy link
Author

aarosil commented Feb 11, 2015

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?

@yads
Copy link

yads commented Feb 12, 2015

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.

@vkarpov15
Copy link
Collaborator

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.

@vkarpov15 vkarpov15 added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Feb 12, 2015
@aarosil
Copy link
Author

aarosil commented Feb 12, 2015

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.

@aarosil aarosil closed this as completed Feb 12, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

No branches or pull requests

3 participants