Kiraahmad
Kiraahmad

Reputation: 159

req.params returns undefiened

I'm trying to get back the ID from the params but it keeps sending back undefiened, what would be the problem here and how can i solve it ?

this is the route:

app.delete(`${api_version}/delete-branch/:id`, verifyToken, branches.deleteBranch)

this is the controller:

exports.deleteBranch = (req, result) => {
const {branch_id}  = req.params
console.log(branch_id) // => returns undefined
if(branch_id === undefined) {
    result.status(404).send({
        message: 'This branch does not exist',
        statusCode: 404
    })
} else {
    // console.log(req.params)
    Branches.deleteBranch(branch_id, (err, data) => {
        if (err) {
            result.status(500).send({
                message: err.message
            })
        } else { 
            result.status(200).send({
                message: 'Branch deleted successfully',
                statusCode: 200,
                data
            })
        }
    })
}
}

Upvotes: 2

Views: 43

Answers (1)

Naor Levi
Naor Levi

Reputation: 1813

You need to destruct req.params like this:

const {id} = req.params instead of:
const {branch_id} = req.params

Or either defined the route as follow:
app.delete(`${api_version}/delete-branch/:branch_id`, verifyToken, branches.deleteBranch)
and then destruct const {branch_id} = req.params;

Upvotes: 2

Related Questions