问题描述
我认为它可以与删除请求类似地工作,因为它以特定用户为目标(删除请求可以正常工作),因此我也向我的get请求传递了一个ID。我记录了请求的响应,并从文件中的catch块得到了错误,在该文件中我使用Knex定义了SQL查询,所以我相信我的SQL查询有问题吗?。
在记录的响应中,我还可以看到想要显示的用户ID,这是一个好兆头,因此,我认为它必须是我的查询或axios请求中显示用户的某个小错误。 / p>
我提供了我单击显示用户按钮时记录的响应照片: logged response from get request
// display specific User
exports.userdisplay = async (req,res) => {
// Find apecific user in database and display
knex('users')
.where('id',req.body.id) // find correct record based on id
.then(() => {
// Send users extracted from database in response
res.json( { message: `This is the data we found: ${req.body.id} ` })
})
.catch(err => {
// Send a error message in response
res.json({ message: `There was an error retrieving user: ${err}` })
})
}
// display User
const handleUserdisplay = async (id: number,name: string) => {
// Send GET request to 'users/all' endpoint
axios
.get("http://localhost:8000/users/display",{ data: {
id: id
} })
.then((response) => {
// Update the users state
setdisplayedUsers(response.data);
console.log(response)
// Update loading state
setLoading(false);
})
.catch((error) =>
console.error(`There was an error retrieving the user list: ${error}`)
);
};
// Remove specific user
exports.usersDelete = async (req,res) => {
// Find specific user in the database and remove it
knex('users')
.where('id',req.body.id) // find correct record based on id
.del() // delete the record
.then(() => {
// Send a success message in response
res.json({ message: `User ${req.body.id} deleted.` })
})
.catch(err => {
// Send a error message in response
res.json({ message: `There was an error deleting ${req.body.id} User: ${err}` })
})
}
解决方法
fields
请求没有正文。至少从理论上讲,任何HTTP请求都可以,但是您在错误的位置寻找值。通常会在URL中发送ID,因此要进行路由匹配:
declared_fields
您将发送:
GET
我想使用的是这样的东西:
/users/display/:id
在服务器端,您将收到/users/display/12345
作为ID。当然,我已经假设您正在使用Express,但是您似乎确实在使用!