1

I have got strange error in nodejs.

My application works perfectly when the order is

app.get('/api/:pid', productController.getColor);

app.get('/', homeController.index);
app.get('/:category/:subcategory/:category_product/:product', productController.index);
app.get('/:category/:subcategory/:category_product', categoryController.products);
app.get('/:category/:subcategory', categoryController.subcategory);
app.get('/:category', categoryController.index);

But when i change the order to

app.get('/', homeController.index);
app.get('/:category/:subcategory/:category_product/:product', productController.index);
app.get('/:category/:subcategory/:category_product', categoryController.products);
app.get('/:category/:subcategory', categoryController.subcategory);
app.get('/:category', categoryController.index);

app.get('/api/:pid', productController.getColor);

My website crashes. Error is so irrelevant. I dont know why does it happen

I guess the problem occurs when nodejs checking all urls and /api/65625499 (i made up the id) fits with '/:category/:subcategory' so it goes crash.

I cant make '/:category/:subcategory' static because i dont know what category would be choosen etc.

How can i solve this situation? Thanks

ERROR

/home/berkin/Projects/OSF_Academy/node_modules/mongodb/lib/utils.js:132
      throw err;
      ^

TypeError: Cannot read property 'categories' of undefined
    at /home/berkin/Projects/OSF_Academy/controllers/categoryController.js:28:40
    at /home/berkin/Projects/OSF_Academy/node_modules/mongoose/lib/model.js:3930:16
    at cb (/home/berkin/Projects/OSF_Academy/node_modules/mongoose/lib/query.js:1314:14)
    at result (/home/berkin/Projects/OSF_Academy/node_modules/mongodb/lib/utils.js:413:17)
    at executeCallback (/home/berkin/Projects/OSF_Academy/node_modules/mongodb/lib/utils.js:405:9)
    at handleCallback (/home/berkin/Projects/OSF_Academy/node_modules/mongodb/lib/utils.js:128:55)
    at self.close (/home/berkin/Projects/OSF_Academy/node_modules/mongodb/lib/cursor.js:934:60)
    at handleCallback (/home/berkin/Projects/OSF_Academy/node_modules/mongodb/lib/utils.js:128:55)
    at completeClose (/home/berkin/Projects/OSF_Academy/node_modules/mongodb/lib/cursor.js:1073:14)
    at Cursor.close (/home/berkin/Projects/OSF_Academy/node_modules/mongodb/lib/cursor.js:1086:10)
    at /home/berkin/Projects/OSF_Academy/node_modules/mongodb/lib/cursor.js:934:21
    at handleCallback (/home/berkin/Projects/OSF_Academy/node_modules/mongodb-core/lib/cursor.js:178:5)
    at setCursorNotified (/home/berkin/Projects/OSF_Academy/node_modules/mongodb-core/lib/cursor.js:555:3)
    at /home/berkin/Projects/OSF_Academy/node_modules/mongodb-core/lib/cursor.js:664:16
    at queryCallback (/home/berkin/Projects/OSF_Academy/node_modules/mongodb-core/lib/cursor.js:242:16)
    at /home/berkin/Projects/OSF_Academy/node_modules/mongodb-core/lib/connection/pool.js:541:18

In a nutshell, error is in codes of subcategory

Community
  • 1
  • 1
Berkin
  • 1,565
  • 5
  • 22
  • 48
  • Is there a way to make the url's along the lines of `/categories/:category/...` ? I'm very sure having parameters as as the first match after '/' could be disastrous in terms of matching. I feel like restful principles aren't being applied – vapurrmaid Mar 07 '18 at 00:04
  • If i cant find any solution, i would do it but i dont want to add extra urls in it. Thanks by the way – Berkin Mar 07 '18 at 00:24

1 Answers1

1

About reading errors

From the error code, all I see the error is in code from this file, which is not shared here.

/home/berkin/Projects/OSF_Academy/controllers/categoryController.js:28:40

I'd suggest checking that file since it's saying it cannot find something within that line.

Now, back to your routes. The error is relevant, because of two reasons,

Ordering of Routes

The ordering of routes is very important. It's always first come, first serve.

If you have /api/:something first and then /:category, it will match /api first.

Rather than me explaining this again, you can go ahead and read the following in depth answer.

No Error Catching

If there is an error in your route, you should catch it, display it and move to next route. This following is from the actual expressjs docs. On a route, there is request, resource and a next callback.

enter image description here

So I'd create an error handler and catch it, then proceed to next(). The expressJS doc is full of next() callback examples.

Final words

You should be able to structure them the following way which will match the home router first, and then api, finally if anything is left, it will try to match the categories.

app.get('/', homeController.index);
app.get('/api/:pid', productController.getColor); // <-- Put API first
app.get('/:category/:subcategory/:category_product/:product', productController.index);
app.get('/:category/:subcategory/:category_product', categoryController.products);
app.get('/:category/:subcategory', categoryController.subcategory);
app.get('/:category', categoryController.index);
Md. Abu Taher
  • 17,395
  • 5
  • 49
  • 73
  • Thank you very much. Yeah i understand you very well. Thanks alot. I'll use next tomorrow now i have to sleep :D – Berkin Mar 07 '18 at 00:38
  • Also file needs some variables for example mens/mens-clothes etc. and insted of mens and mens-clothes, api and pid comes in then error occurs. – Berkin Mar 07 '18 at 00:40