Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

node.js - 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
            })
        }
    })
}
}
question from:https://stackoverflow.com/questions/65890493/req-params-returns-undefiened

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...