如何解决:主义无法正确返回

时间:2019-05-26 08:54:58

标签: javascript symfony

我想使动态删除帖子功能正常,但由于值为空,结果是错误。

控制器

/**
  * @Route("/posts/delete/{id}", name="delete_post", methods={"DELETE"})
  */
  public function deletePost($id, LoggerInterface $logger)
  {

    $post=$this->getDoctrine()->getRepository(Article::class)->find($id);

    if(!$post)
    {
        $this->addFlash(
            'notice',
            'Something went wrong'
        );
        $logger->info($id);
    }
    else
    {

        $entityManager=$this->getDoctrine()->getManager();
        $entityManager->remove($post);
        $entityManager->flush();
        $entityManager->clear();
    }

    $response=new Response();
    return($response);
  }

JS

const articles = document.getElementById("articles");

if (articles) {
 articles.addEventListener("click", e => {
  if (e.target.className === "btn btn-danger delete-article") {
  if (confirm("Are you sure?")) {
    const id = e.target.getAttribute("data-id");

    fetch("/posts/delete/${id}", {
      method: "DELETE"
    }).then(res => window.location.reload());
  }
 }
 });
}

树枝

<table border="1" id="articles">
        <tr>
            <th class="pt">Post title</th>
            <th class="pe">Edit</th>
            <th class="pd">Delete</th>
        </tr>
        {% for article in articles %}
            <tr>
                <td class="title">{{ article.title }}</td>
                <td class="edit">
                    <a href="{{ path('posts_editor', {'id':article.id}) }}" class="btn btn-primary edit-article">
                        <img src="{{ asset('images/edit.png') }}" class="image">
                    </a>
                </td>
                <td class="delete">
                    <a href="#" class="btn btn-danger delete-article" data-id="{{article.id}}">
                        <img src="{{ asset('images/delete.png') }}" class="image">
                    </a>
                </td>
            </tr>
        {% endfor %}
    </table>

我正在通过视频教程进行操作,我不知道这段代码在视频中有什么问题,但是当我编写它时,我在下面出现了一个错误

错误

  

EntityManager#remove()期望参数1是一个实体对象,给定NULL。

希望任何人都可以帮助我,因为无法正常工作,我真的很沮丧。

2 个答案:

答案 0 :(得分:0)

很明显, $ post 为空,因此在这种情况下,您可以 转储这两个变量,希望您能弄清楚发生了什么

dump($id);
dump($post);
exit

您可以查看文档Doctrine Removing entities

已更新:

在您的代码中,您始终会发送响应,您需要将响应更新为特定的成功或错误代码,默认情况下,它会发送200个代码,并且还需要更新代码

public function deletePost($id, LoggerInterface $logger)
{

$post=$this->getDoctrine()->getRepository(Article::class)->find($id);

    if(!$post)
    {
        $this->addFlash(
            'notice',
            'Something went wrong'
        );
        $logger->info($id);

        exit() your code here
        or throw an exception here
    }

    try {
        //remove entity here 
        $response = new Response(
            'Content',
            Response::HTTP_OK,
            ['content-type' => 'text/html']
        );

        return $response;
    } catch (\Exception $exception) {
        $response = new Response(
            $exception->getMessage(),
            Response::HTTP_INTERNAL_SERVER_ERROR,
            ['content-type' => 'text/html']
            );
        return $response;
    }
}

答案 1 :(得分:0)

已修复

只需更改此

fetch("/posts/delete/${id}", {
  method: "DELETE"
}).then(res => window.location.reload());

在此

fetch("/posts/delete/" + id, {
      method: "POST"
    }).then(res => window.location.reload());

现在它正在正确删除帖子