res.status 不是函数 - express
res.status is not a function - express
我有获取用户详细信息的途径。这是控制器:
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 !" })
}
})
}
}
当我向 url 发出请求时,出现以下错误:
TypeError: res.status is not a function
at Query.<anonymous> (/Applications/MAMP/htdocs/my-app/controllers/auth.controller.js:9:21)
第 9 行是 res.status(200).json({ res })
这个方法有没有错误?
谢谢
如果我根据您的评论正确理解您的问题。您必须更改您在 sql query callback
中使用的变量,而不是您在 getUser
函数
中接收到的变量
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 !" })
}
})
}
}
像这样的东西应该有用。
我有获取用户详细信息的途径。这是控制器:
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 !" })
}
})
}
}
当我向 url 发出请求时,出现以下错误:
TypeError: res.status is not a function
at Query.<anonymous> (/Applications/MAMP/htdocs/my-app/controllers/auth.controller.js:9:21)
第 9 行是 res.status(200).json({ res })
这个方法有没有错误?
谢谢
如果我根据您的评论正确理解您的问题。您必须更改您在 sql query callback
中使用的变量,而不是您在 getUser
函数
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 !" })
}
})
}
}
像这样的东西应该有用。