尝试使用 Axios get 请求获取特定用户无效。应该是一个小的修复?
Trying to get a specific user using Axios get request not working. Should be a minor fix?
我认为它的工作方式与我的删除请求类似,因为它针对特定用户(删除请求非常有效),所以我也向 get 请求传递了一个 id。我记录了请求的响应,并且在我使用 Knex 定义 sql 查询的文件中的 catch 块中给出了错误,所以我相信我的 sql 查询有问题吗?。
在记录的响应中,我还可以看到我想要显示的用户的 ID,这是一个好兆头,所以我认为它必须是我的查询或 axios 请求中某个地方的小错误才能显示用户.
我提供了单击显示用户按钮时记录的响应的照片:
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}` })
})
}
GET
请求没有正文。至少,理论上任何 HTTP 请求都可以,但您在错误的地方寻找您的价值。通常会在 URL 中发送 ID,因此对于路由匹配:
/users/display/:id
你会发送:
/users/display/12345
我想,使用类似的东西:
axios.get(`http://localhost:8000/users/display/${id}`)
在服务器端,您收到的 ID 为 req.params.id
。当然,我假设您正在使用 Express,但您似乎确实在使用!
我认为它的工作方式与我的删除请求类似,因为它针对特定用户(删除请求非常有效),所以我也向 get 请求传递了一个 id。我记录了请求的响应,并且在我使用 Knex 定义 sql 查询的文件中的 catch 块中给出了错误,所以我相信我的 sql 查询有问题吗?。
在记录的响应中,我还可以看到我想要显示的用户的 ID,这是一个好兆头,所以我认为它必须是我的查询或 axios 请求中某个地方的小错误才能显示用户.
我提供了单击显示用户按钮时记录的响应的照片: 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}` })
})
}
GET
请求没有正文。至少,理论上任何 HTTP 请求都可以,但您在错误的地方寻找您的价值。通常会在 URL 中发送 ID,因此对于路由匹配:
/users/display/:id
你会发送:
/users/display/12345
我想,使用类似的东西:
axios.get(`http://localhost:8000/users/display/${id}`)
在服务器端,您收到的 ID 为 req.params.id
。当然,我假设您正在使用 Express,但您似乎确实在使用!