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 })
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.