简体   繁体   中英

MEAN.JS — Request objects, Express controllers, and Mongoose Schemas

I have created a Mongoose connection manager to manage a unique connection per user in my application. At the time that a user logs in, I create a new connection to a custom database URI that is unique to that user. I name the connection so that it can be retrieved (looked up) later. Example invocation:

var myConnection = require('connection-manager').createConnection(user._id);

As you can see here, I name my connections after an _id property of a user object. Easy enough. I can then look up my unique connection for this user using:

var myConnection = require('connection-manager').getConnection(user._id);

The only difference between those two methods is that one creates and returns the new connection and the other (the getConnection() method) does a simple look up in the pool of current connections and returns the connection object that matches our passed name (user._id).

Here's where I'm running into problems. In my server controllers, I normally do this:

var mongoose = require('mongoose'),
    Foo = mongoose.model('Foo');

This is really straight forward. I'm loading the model, 'Foo', that has been registered with the default Mongoose connection. This is handy because all methods of my controller can simply invoke this model by doing something like:

exports.create = function(req, res, next) {
    myFoo = new Foo({whatever: whatever});
};

This works because Foo was declared and loaded at the start of the file. But now that I'm moving to a connection manager, I need to be able to do something like this in my controller file:

'use strict';

var manager = require('connection-manager').getConnection(user._id),
    Foo = manager.model('Foo');

exports.create = function(req, res, next) {
    var myFoo = new Foo({whatever: whatever});
};

The problem is that the request object that gets passed to exports.create() is where the user._id is that I need in order to look up my custom connection in the connection manager. How do I work around this..?

The code you have in your question that gets a connection for the create method only runs once, the first time you require that file. That is why it doesn't make sense to use the userid at that point, because there is no user at that point, the server might not even be accepting request at that point. Instead, you need to get the connection on each request, meaning, inside the create method.

'use strict';

var manager = require('connection-manager');

exports.create = function(req, res, next) {
    var connection = manager.getConnection(req.user._id),
        Foo = connection.model('Foo'),
        myFoo = new Foo({whatever: whatever});
};

In your code above:

app.route('/someroute').post(foo.create);

It pipes the request directly to foo.create, which is kinda confusing for beginners, what it really means is:

app.post('/someroute', function(req, res, next) {
    //get your data here
    //user = req.user_id;
    foo.create(req, res, next);
});

My suggestion is, don't use MEAN stack to begin with, create a simple express application and figure out how the routes works.

And use express.Router() instead of app.route .

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM