Question

Is it possible to "insert" a missing parameter (in case it was not sent), in order to persist them all via req.params.all()?

Currently trying to do so, gets me a no method 'setParameter' error.

Was it helpful?

Solution 2

It's not possible to add anything into the result of req.params.all() in Sails without implementing custom middleware, since it's parsed at request time and provided to route handlers before any user-level code runs. However, if what you're looking for is default parameter options, in Sails v0.10.x you can use the req.options hash for this. If you wanted to set a default limit for a blueprint "find", for example, you could do the following in your /config/routes.js:

'/user': {blueprint: 'find', options: {limit: 10}}

Documentation for these options is forthcoming, in the meantime you can examine the source code to see which blueprint options are available.

For default params in your custom controller actions, you can use options in a similar way:

'/foo': {controller: 'FooController', action: 'bar', options: {abc: 123}}

and in your action code, check the options:

bar: function(req, res) {

   var abc = req.param('abc') || req.options.abc;

}

This is preferable to messing with req.params.all, because you can tell whether you are using a user-supplied or a default value. But if you really want to alter the params directly, you can do it through custom middleware in /config/express.js:

customMiddleware: function(app) {

   app.use(function(req, res, next) {

       // Note: req.params doesn't exist yet because the router middleware
       // hasn't run, but we can add whatever we want to req.query and it
       // will stick as long as it isn't overwritten by a route param or
       // something in req.body.  This is the desired behavior.
       req.query.foo = req.query.foo || 'bar';
       return next();

   });

}

OTHER TIPS

In case anyone might need this.

I just had to deal with a similar issue where I had to translate some parts of a URL from multiple languages to English before the flow had reached the controller.

First of all I'm using Sails v0.10.5 and I don't know how long back this goes.

At this version the recommended way of adding middleware is using either a policy or the http config. At first I tried creating a policy but Sails doesn't allow you to change parameters inside a policy. And thus I resorted to http. This configuration offers the possibility to add your own middleware for http requests.

In there you'll find the middleware: property to which you can add your own function and then insert it into the order array:

order: [
        'startRequestTimer',
        'cookieParser',
        'session',
        'myRequestLogger',
        'bodyParser',
        'handleBodyParserError',
        'compress',
        'methodOverride',
        'poweredBy',
        '$custom',
        '__your_function',
        'router',
        'www',
        'favicon',
        '404',
        '500'
    ]

So I added my own function which subscribes to the router:route event so that the params array is already populated.

addRouterListener: function (req, res, next) {
    sails.on('router:route', function (args) {

        // Proceed to changing your params here
        // It would also be wise to check the route or the controller
        if (args.options.controller === 'x' && args.req.params['type']){
            args.req.params['type'] = translation[args.req.params['type']];
        }

    });

    return next();
}

This one works for me for example in a controller where I would like to create new items from models for a DB insert, etc:

var requestParams = req.params.all();
requestParams.newParameter = 'XYZ';
requestParams.whatever = '123';

And use requestParams object where you need all parameters + the additional parameters. For example:

MyModel.create(requestParams, function(err, user) {
  ...
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top