Comments (5)
I've just run into this as well. I think it is due to commit 02e6f01.
Mongoose's behavior is to hang if there is not a connection when you tell it to do something. If your model module just does the standard User = mongoose.model('User',UserSchema)
then it gets attached to the first connection for mongoose. Later on when you call save()
it is against this connection that has never been initialized (since migrate-mongoose creates and initializes its own connection.) If you add something like this
var mongoose = require('mongoose');
if (mongoose.connection.readyState != mongoose.STATE_OPEN)
{
mongoose.connect(process.env.MONGODB_URI); // or wherever you're getting this from
}
var User = require('../models/user.js').User;
to the beginning of each migration, it'll make sure it is connected. I don't know if there is a better place to put such code, but this got me past this problem after struggling with it for a few hours. (I put it in the migration since I didn't want to clutter the app's code since the app can always assume a connection is there.)
I'm not sure what the reasoning of the prior commit was, but the docs should probably get updated to alert users because it sure is a non-obvious behavior.
from migrate-mongoose.
@bastienar The answer depends on how you registered your model in ../models/user
if you did mongoose.model('user', UserSchema)
, then you'd do this('user')
in your code, not this('User')
The migration framework opens its own connection to mongoose, and exposes mongoose.model
as this
. The reason your models still work on this
is because mongoose exports a singleton which is what both your code and my framework use to register models (As far as my understanding goes).
You can try what I mentioned above, but I recommend just doing the import as you're doing and not assigning it from this('whatever')
You can also do away with the callbacks and just return user.save().then()...
because we have promise support :)
Let me know how that works out, or if you can suggest a way to improve the way it's set up so it's less confusing. Do we need to change the architecture? More documentation explaining something specific? An example?
Cheers
from migrate-mongoose.
@gt6796c thanks for sharing your solution.
I can't get this('myModel') access to work no matter what I do, so after some investigation it seems to me that it might have broken after that commit you mentioned,
I have created an issue about it. See: #13
from migrate-mongoose.
This should be fixed in > 2.1.0
from migrate-mongoose.
This is still an issue for me. I'm using 3.2.2.
I have to do @gt6796c suggested.
var mongoose = require('mongoose');
if (mongoose.connection.readyState != mongoose.STATE_OPEN)
{
mongoose.connect(process.env.MONGODB_URI); // or wherever you're getting this from
}
var User = require('../models/user.js').User;
Any suggestion to use the default connection instead of opening a new one?
from migrate-mongoose.
Related Issues (20)
- New Server Discover and Monitoring engine for MongoClient HOT 2
- Cannot find module '@/enums/roles'
- Use NPX instead of referencing the node module
- Ability to specify migrations for a given NODE_ENV, or support seeding for local versus migrations for production
- Microservice environment
- Are ES6 imports supported in 4.0.0? HOT 2
- Deprecation warning HOT 1
- Support Migration files with *.ts extension HOT 4
- Is this repo still maintained? HOT 4
- How do we setup migrate.json HOT 1
- I am getting "Cannot use import statement outside a module" HOT 4
- can i use migrate-mongoose in midway framework?
- up never get's past "Synchronizing database with file system migrations" HOT 1
- Unexpected token { when using the package in typescript project
- Is there a way to grab the `connectionUri` or any other useful information in the `up` or `down` functions?
- Schema hasn't been registered HOT 1
- Security vulnerability on dangling dependency of yargs "^4.8.1"
- Compromised colors package HOT 1
- Engine warning and timeout HOT 6
- Generate initial migrations?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from migrate-mongoose.