[英]res.status is not a function - express
我有一條獲取用戶詳細信息的路線。 這是 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 !" })
}
})
}
}
當我向 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
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 !" })
}
})
}
}
這樣的事情應該有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.