eixcs
eixcs

Reputation: 1989

MongoDB, Nodejs routing order

I have the problem with my routes. They work but only the one that's in the code first. The code below allows me to get a ticket by ID but not by registration number. If I put the second route above the first one it's vice versa.

What can I do to fix this?

//Get a ticket by ID

app.get('/tickets/:_id', function(req, res){
    Ticket.getTicketById(req.params._id, function(err, ticket){
        if(err){
            throw err;
        }
        res.json(ticket);
    });
});

//Get a ticket by registration number

app.get('/tickets/:vehRegistration', function(req, res){
    Ticket.getTicketByReg(req.params.vehRegistration, function(err, ticket){
        if(err){
            throw err;
        }
        res.json(ticket);
    });
});

Upvotes: 1

Views: 36

Answers (1)

mehta-rohan
mehta-rohan

Reputation: 1341

You have written the same API path twice. :param_name can't be used to differentiate between two different paths. Try

//Get a ticket by ID

app.get('/tickets/byid/:_id', function(req, res) {
    Ticket.getTicketById(req.params._id, function(err, ticket) {
        if (err) {
            throw err;
        }
        res.json(ticket);
    });
});

//Get a ticket by registration number

app.get('/tickets/byreg/:vehRegistration', function(req, res) {
    Ticket.getTicketByReg(req.params.vehRegistration, function(err, ticket) {
        if (err) {
            throw err;
        }
        res.json(ticket);
    });
});

Upvotes: 1

Related Questions