1

我使用 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 对象

编辑:

我注意到只有当我有多个对象时才会出现此错误??如果我只有一个,我删除它,没有错误

4

1 回答 1

1

状态 500 是服务器错误,从行为看来,无论导致错误的原因都必须在删除项目后发生。

查看该deleteWish方法,如果$wish已删除,则问题可能是在未定义时尝试将其转换为 JSON。尝试返回其他内容,例如:

return true;
于 2020-04-07T09:00:41.660 回答