Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

res.status is not a function – express

I have a route to get user details. Here is the controller :

module.exports.getUser = (req, res) => {
    if (req.method == "GET") {
        const userDetails = `SELECT * FROM users WHERE id = ${req.params.id}`;

        sql.query(userDetails, function (err, res) {
            if (res.length > 0) {
                res.status(200).json({ res })
            } else {
                res.status(401).json({ message: "Error with this id !" })
            }
        })
    }
}

When i make a request to the url, I have this error :

TypeError: res.status is not a function
    at Query.<anonymous> (/Applications/MAMP/htdocs/my-app/controllers/auth.controller.js:9:21)

Line 9 is res.status(200).json({ res })

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there an error in this method ?

Thank you

>Solution :

If I understand your issue correctly, as per your comments. You have to change the variable you are using in sql query callback not the one you are receiving in getUser function

module.exports.getUser = (req, res) => {
    if (req.method == "GET") {
        const userDetails = `SELECT * FROM users WHERE id = ${req.params.id}`;

        sql.query(userDetails, function (err, user) {
            if (user.length > 0) {
                res.status(200).json({ user })
            } else {
                res.status(401).json({ message: "Error with this id !" })
            }
        })
    }
}

Something like this should work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading