Git Product home page Git Product logo

Comments (5)

gt6796c avatar gt6796c commented on September 26, 2024 2

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.

balmasi avatar balmasi commented on September 26, 2024

@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.

jay-jlm avatar jay-jlm commented on September 26, 2024

@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.

balmasi avatar balmasi commented on September 26, 2024

This should be fixed in > 2.1.0

from migrate-mongoose.

paventuri avatar paventuri commented on September 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.