0

I am using Sails JS, PostgreSQL stack.

I want to pass two ids as parameters at the end of a url as below:

/getLocation/cc/re

Where 'cc' is a two letter country code e.g US and 're' is a an autocomplete query for a region of the country referred to in 'cc'

While using only cc works , adding the extra parameter results in a 'Not Found' page.

Here is the 'config/routes':

'get /location/getRegion/:cc/:re': 'LocationController.getRegion'

Pointing to say:

'localhost/location/getRegion/us/or'

results in a 404.

While:

'localhost/location/getRegion/us'

Works but obviously this outputs all regions/states in the us. The second parameter is supposed to filter this result further using 'contains'

Any ideas on how I can achieve the desired result using the two parameters?

Here is the

getRegion

code in my

LocationController

module.exports={
        getRegion:function(req, res, next){
        Regions.find({where:{country_code:req.param('cc')},region:{'contains':req.param('id')}},function selectRegions(err, regions){
            console.log(req.param('cc'))
            if(err) return next(err);
            if(!regions) return next();
            console.log(regions)
            res.json(regions)
        })
    }   
}
watkib
  • 347
  • 3
  • 11
  • 25

1 Answers1

0

Your controller code is a good demonstration of why we discourage using next in Sails controller actions: it can make things difficult to debug. In your case, you've got the following in your find criteria:

region:{'contains':req.param('id')}

where you probably meant req.param('re'), since your route doesn't provide an id parameter. Thus the query will always return no results, and you end up calling next(), which returns control to the Sails router which doesn't match any other routes and bingo, 404. If you changed this:

if(!regions) return next();

to

if(!regions) return res.notFound();

it would have the same effect, but you'd probably have caught on sooner what was going on.

sgress454
  • 24,870
  • 4
  • 74
  • 92