8

I have an Node.js Express web app running.

I have two database connections, the system_db is created with "Mongoose.connect", the user_db is created with "Mongoose.createConnection". This seems to make them separate and have different connection pools. Although this could be the crux of the problem.

I have code dealing with with a Mongoose model

in the model file I have:

var authSchema = mongoose.Schema({
    teamName: String,
    league: String,
    players: []

});


var Team = module.exports = mongoose.model('teams',authSchema);

in my main file I have two connections:

one is a system database

connection_uri = "mongodb://localhost:27017/";
system_db = mongoose.connect(connection_uri.concat("sparks"), {
    native_parser : true
}, function(err){
    if (err) throw err;
});

the other is a user database

 user_db = mongoose.createConnection(connection_uri.concat(user_id));

then I have this piece of code in my main file which finds a Team object based off id:

app.param('team_id', function(req, res, next, team_id) {

    Team.findOne(team_id, function(err, team) {

        if (err) {
            return next(err);
        }
        if (!team) {
            throw new Error("no team matched");
        }
        req.team = team;
        next();
    });
});

the problem is that the app.param function above is not finding any matches for teams, even though they exist in a collection in the user_db database. this means, that I am thinking my model is pointing to the wrong database somehow? Somehow it must be pointing to this system_db instead of the user_db. Could this be right? How would I fix that?

1 Answer 1

7

The below method opens a connection to the system_db and is binded to the mongoose object,.i.e the current mongoose object is modified. The return type is not a Connection.

system_db = mongoose.connect(connection_uri.concat("sparks")...

Now when you again do:

user_db = mongoose.createConnection(connection_uri.concat(user_id));

This creates a new Connection to the user database and returns a connection, but does not modify the mongoose instance. The mongoose instance is still binded to the system_db database connection.

Since Team model is obtained from the same mongoose instance,

var Team = mongoose.model('teams',authSchema);

whatever operation is done on this model effectively occurs in the connection that the mongoose instance holds, and that is of the system_db database.

So you need to obtain the model from the user_db connection :

var user_db = mongoose.createConnection(..);
// retrieve the Team model
var Team= user_db.model('teams'); // not from mongoose instance.

use createConnection wherever you want to obtain a connection.

Sign up to request clarification or add additional context in comments.

6 Comments

hmmm, thanks, this is discouraging. I want to be able to load that file (with Team defined in it) before the user_db variable is defined...
furthermore, if the user logs out, and logs in as a different user, the user_db variable would be redefined.
in an ideal universe, I could pass the save method the database variable, so it would be var team = new Team({}); team.save(user_db); you know what I am sayin'?
Why are you maintaining a different database for each user? You should make your database queries specific to the user rather than maintaining a database for each user. That way the Model Team will be common to all the users.
turns out having each user in a different DB is a bad idea
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.