Home > database >  How to get the route link work with same text?
How to get the route link work with same text?

Time:10-20

I do not know how to ask this question in a better way, so I have this route. I have details/user_id and detail/id but when I make both of them details...it looks like it does not care about the route below the main one. How to make it differentiate between details/id and details/user_id?

const router = require('express').Router();
const db = require('../models');

// Get details by user_id
router.get('/details/:user_id', (req, res) => {
    try {
        db.Customer.findAll({
            where: {
                user_id: req.params.user_id,
            },
        }).then((user) => res.send(user));
    } catch (err) {
        res.status(500).json(err);
    }
});

// Get details by id
router.get('/detail/:id', (req, res) => {
    try {
        db.Customer.findAll({
            where: {
                id: req.params.id,
            },
        }).then((user) => res.send(user));
    } catch (err) {
        res.status(500).json(err);
    }
});

module.exports = router;

CodePudding user response:

If the parameter is not the same / same type you should use a different route

for instance :

router.get('/details/users/:user_id', (req, res) => {
    try {
        db.Customer.findAll({
            where: {
                user_id: req.params.user_id,
            },
        }).then((user) => res.send(user));
    } catch (err) {
        res.status(500).json(err);
    }
});

CodePudding user response:

I think what you want in some ways can't be done. For example if you have in your db, the following schema,

user_id : uuid

id: uuid

you see that both of them have the same pattern? so router would be confused an get your first function for any value. Why not use a route like Jimmy Soussan suggested, use a route like this /details/users/:id for users and another /details/rooms/:id.

But if you really want to implement something like that I guess that you could do it by using regex. If your id and user_id are of different types then you could use regex to filter them. the documentation for the image bellow https://expressjs.com/en/guide/routing.html

So Let's suppose you go forward trying to use regex and user_id is a serial type and id is an uuid type.

uuid regex: [a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}

router.get('/details/users/:user_id(\[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}\)', (req, res) => {
try {
    db.Customer.findAll({
        where: {
            user_id: req.params.user_id,
        },
    }).then((user) => res.send(user));
 } catch (err) {
    res.status(500).json(err);
 }
});

So it would only function if the value passed to the url is an uuid. That way you could filter by its value, but remember if the types are equal than that's not very helpful because the values would follow the same pattern. You could use a website online like https://regex101.com/ to test your values against regex.

Useful Content for your problem:

https://expressjs.com/en/guide/routing.html

Searching for UUIDs in text with regex

https://regex101.com/

I hope I was helpful on my answer and good luck with your problem. : )

  • Related