我已经使用axios.delete()在前端执行删除,代码如下
Axios({
method: 'DELETE',
url:'http://localhost:3001/delete',
params: {
id:id,
category:category
}
})我在后端使用了koa-router来解析我的请求,但是我不能得到我的查询参数。
const deleteOneComment = (ctx,next) =>{
let deleteItem = ctx.params;
let id = deleteItem.id;
let category = deleteItem.category;
console.log(ctx.params);
try {
db.collection(category+'mds').deleteOne( { "_id" : ObjectId(id) } );
}
route.delete('/delete',deleteOneComment)有人能帮我一下吗?
发布于 2018-04-06 08:49:51
基本上,我认为你误解了context.params和query string。
我假设您使用的是koa-router。使用koa-router,可以将params对象添加到koa context,并提供对named route parameters的访问。例如,如果使用命名参数id声明路径,则可通过params访问该路径
router.get('/delete/:id', (ctx, next) => {
console.log(ctx.params);
// => { id: '[the id here]' }
}); 要让查询字符串通过HTTP,您需要使用ctx.request.query,它的ctx.request是koa请求对象。
另一件你应该意识到的事情是,基本上,http delete请求不推荐有正文,这意味着你不应该传递params。
发布于 2020-06-11 12:35:59
您可以使用ctx.query,然后输入所需的值的名称。
例如,对于给定的url:
https://hey.com?id=123
您可以使用ctx.query.id访问属性id。
router.use("/api/test", async (ctx, next) => {
const id = ctx.query.id
ctx.body = {
id
}
});发布于 2018-04-06 07:41:05
每个koa documentation ctx.request.query
https://stackoverflow.com/questions/49663137
复制相似问题