seanemmer / mongoose-seed

Seed data population for Mongoose
MIT License
52 stars 33 forks source link

Which path to use for loadModels? #13

Closed joetidee closed 7 years ago

joetidee commented 7 years ago

I am loading in models to the seeder.loadModels() function like so:

seeder.loadModels([
    path.join(__dirname, '../server/models/model.js')
]);

The above path equates to /server/models/model.js, which seems like it should be correct. However, whe the function runs, it pukes:

Error: Cannot find module '/server/models/model.js'

so why is it not working?

(all paths below are relative to the applicaiton root) The model files are stored here /server/models The above loadModels function is being used in a file located here /seed/seed.js Finally, seed.js is being imported into a file located here /client/tests/unit/globalBefore.js

joetidee commented 7 years ago

Just realized that the reason I might be getting this error is because I am using the mongoose-seed package for trying to generate some fixtures prior to running some client-side mocha tests. Would you say this is the reason?

seanemmer commented 7 years ago

Hi Joe,

I think your intuition here is correct - the models are loaded from the path relative to where the module is being executed. Based on what you said you need to specify the path relative to your testing file.

Best, Sean

joetidee commented 7 years ago

How can I use a file, that contains a seeder.loadModels block, in two different locations across my application, if the paths have to be relative to the script that is including them?

seanemmer commented 7 years ago

The loadModels code is a wrapper around the path.resolve node.js method. You should be able to pass an absolute path to this without issue.