Ajax request 500 error – Call to a member fucntion on null

I have my Ajax. method within Symfony which sends id of the clicked button in specific table row.

My error log returns:

Call to a member function changeStatus() on null

This is strange, because when I dump($id) in my Controller, it’s shows the id of that entity object, so I can not figure out where the problem is.

This is my method:

/**
  * @Route("/my-entity-route/{id}", name="change_status", options={"expose"=true})
  */
    public function changeStatus($id)
    {
       // dump($id);die; -- shows id number

        $entity = $this->entityManager->getRepository(MyEntity::class)->find($id);

        $entity->setStatus(MyEntity::STATUS_CHANGE);
        $this->entityManager->persist($entity);
        $this->entityManager->flush();
    
    }
}

And my button:

<button type="button" data-entity_id="{{ item.id }}" class="change">Switch Status</button>

And method in js file:

$(".change").click(function(ev, el){
var id = $(this).data("entity_id");
if (confirm("Are you sure that you want change status?")) {
    changeToNewStatus(id);
 }
});

function changeToNewStatus(id) {
    $.ajax({
        type: 'PATCH',
        url: "/my-entity-route/"+id,
        processData: false,
        contentType: 'application/json-patch+json',
        success: function () {
            console.log('success!')
        },
        error: function (xhr) {
            var err = JSON.parse(xhr.responseText);
            alert(err.message);
        }
      });
    }

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *