我使用 vue 作为我的前端。我想在按下按钮时从我的数据库中删除一个对象,我使用 axios 发布所选对象,但出现以下错误:
wish.js:40 Error: Request failed with status code 500
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:61)
即使我的对象确实从我的数据库中删除。
这是我的代码:
postWishToBeDeleted({commit}, wishId) {
console.log(wishId);
axios.post('/api/post/delete', {
wishId: wishId
}).catch(error => {
console.error(error);
}).then( response => {
commit('removeWish', wishId);
}
)
}
在我的 symfony 控制器内部:
/**
* @Route("/api/post/delete", name="app_api_post_delete", methods={"POST"})
*/
public function deleteWish(Request $request, WishRepository $repository) {
$data = $request->getContent();
$data = json_decode($data, true);
$wish = $repository->find($data['wishId']);
$em = $this->getDoctrine()->getManager();
$em->remove($wish);
$em->flush();
return $this->json($wish);
}
我认为我的回复有问题,我对 Vue 和 axios 还是新手,所以我不确定如何正确返回 json 对象
编辑:
我注意到只有当我有多个对象时才会出现此错误??如果我只有一个,我删除它,没有错误