Home > database >  Express routing returns 404 despite route being declared
Express routing returns 404 despite route being declared

Time:03-02

So on my backend server, I have declared a route that accepts params

This is the route:

router.route('/freeze-check/:region').get(freezeCheck);

freezeCheck is just a controller that pulls from the db that filters out products with the regionId.

On my front end I called this route by doing this:

const adminRegion = props.authData.admin.region.id;
    const res = await apiCaller.get(`/products/freeze-check`, { params: { region: adminRegion } 

apiCaller is just axios exported with some settings. I console logged adminRegion multiple times and it returns the expected value, this is a state pulled from redux store however the backend returns with this:

GET /api/products/freeze-check?region=1 404 30.114 ms - 164

Which I don't get since I declared that route in the backend all the base URLS are working fine I can verify this, am I missing a syntax or something? is this because of the old express or axios version?

I'm using:

express: ^4.14.0

axios: ^0.16.2

CodePudding user response:

By looking at your request response,you're not sending the region as params instead as a query.

GET /api/products/freeze-check?region=1

Try to send the param like this

await apiCaller.get(`/products/freeze-check/${adminRegion}`)

if the params is optional you could to something like this. pass an trailing question mark in a route

router.route('/freeze-check/:region?').get(freezeCheck);

the remaining code should be as it should be.

  • Related