question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I probably misundestood something about your module, because this kind of code

'use strict';

var User = require('../models/user.js');

/**
 * Make any changes you need to make to the database here
 */
exports.up = function up(done) {
    User = this('User');
    var user = new User({
        username: "foo"
    });
    user.save()
        .then(function(response) {
            console.log(response);
            done();
        })
        .catch(function(err) {
            console.log(err);
            done(err);
        });
};

seems to block on the call to mongoose save. Did I miss something about how thinks should be done ?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
gt6796ccommented, Nov 1, 2016

I’ve just run into this as well. I think it is due to commit 02e6f018ca9d71e3082b1c819c438630c7ba0b32.

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.

0reactions
paventuricommented, Oct 26, 2017

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

21 Money Saving Challenges to Try in 2022 - Inspired Budget
21 Money-Saving Challenges To Try in 2022 · 1. No-Spend Challenge · 2. 52-Week Money Challenge Backwards · 3. 8-Week Vacation Savings Plan...
Read more >
18 Money-Saving Challenges To Save More Money!
18 money saving challenges to try today · 1. The 6 month savings challenge · 2. 30 days to master your spending challenge...
Read more >
8 Savings Challenges To Try This Year – Forbes Advisor
8 Savings Challenges To Help You Reach Your Money Goals This Year · 1. 52-Week Challenge · 2. Dollar Savings Challenge · 3....
Read more >
20 Money Saving Challenges To Try Before the End of the Year
1. Save the Change Challenge · 2. The $5 Challenge · 3. Trim 1% of Your Salary Challenge · 4. The Weather Wednesday...
Read more >
Shopportunist: Money-saving challenges to try in 2023
Shopportunist: Money-saving challenges to try in 2023. Looking to save more money in the new year? How about participating in a yearlong ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found