我有一條獲取用戶詳細資訊的路線。這是控制器:
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 })
這種方法有錯誤嗎?
謝謝
uj5u.com熱心網友回復:
如果我正確理解您的問題,請按照您的評論。您必須更改您正在使用的變數,而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 !" })
}
})
}
}
像這樣的東西應該作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/465843.html
