ERROR Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":{},"lazyUpdate":null},"status":500

I’m stuck on this error for hours now… I’m such a beginner and I’m developping a web app, with angular for front and symfony5 for back. I’m trying to send a POST request to the symfony server and receive a respond on the angular app here is the way…

How I’m sending from angular :

submit()
  {
    let toSend = {
      id:this.id,
      name:this.name,
      birthday:"2020-06-03",
      age:this.age,
      profession:this.profession,
      adress:this.adress,
      telephone:this.telephone,
      maritalStatus:this.maritalStatus,
      education:this.education,
      spouseName:this.spouseName,
      spouseProfession:this.spouseProfession,
      spouseEducation:this.spouseEducation,
      nationality:this.nationality,
      arrivalDate:this.arrivaldate,
      history:this.history,
    }
    let url = "https://127.0.0.1:8000/api/post/change";
    this.http.post(url, toSend).toPromise().then((data:any) => {
      console.log(data);
    })
  }

How I’m receiving request from angular in symfony and send a respond to angular :

    /**
     * @Route("/api/post/change", name="api_post_change", methods={"POST"})
     */
    public function changePatient(Request $request) : Response
    {
        $receivedRequest = $request->getContent();
        $patientId = (int)explode(":", explode("n", explode(",", $receivedRequest)[0])[1])[1];
        $birthday = explode(""", explode("n", explode(",", $receivedRequest)[2])[1])[3];
        $arrivalDate = explode(""", explode("n", explode(",", $receivedRequest)[13])[1])[3];
        $post = $this->serializer->deserialize($receivedRequest, Demographie::class, 'json');
        $response = $this->updateClient($patientId, $post, $birthday, $arrivalDate);
        $response->headers->set('Access-Control-Allow-Origin', '*');
        return $response;
    }

    public function updateClient(int $patientId, Demographie $infos, $birthday)
    {
        $demographieData = $this->demographieEntity->findOneBy(['id' => $patientId]);
        $demographieData->setName($infos->getName());
        $demographieData->setBirthday(DateTime::createFromFormat('Y-m-d', "2017-05-05"));
        $demographieData->setAge($infos->getAge());
        $demographieData->setProfession($infos->getProfession());
        $demographieData->setAdress($infos->getAdress());
        $demographieData->setTelephone($infos->getTelephone());
        $demographieData->setMaritalStatus($infos->getMaritalStatus());
        $demographieData->setNationality($infos->getNationality());
        $demographieData->setEducation($infos->getEducation());
        $demographieData->setSpouseName($infos->getSpouseName());
        $demographieData->setSpouseProfession($infos->getSpouseProfession());
        $demographieData->setSpouseEducation($infos->getSpouseEducation());
        $demographieData->setArrivalDate(DateTime::createFromFormat('Y-m-d', "2017-05-05")/*DateTime::createFromFormat('Y-m-d', "2018-09-09")*/);
        $demographieData->setHistory($infos->getHistory());
        $this->em->persist($demographieData);
        $this->em->flush();
        $demographieSerialized = $this->serializer->serialize($demographieData, 'json');
        $response = new JsonResponse($demographieSerialized, 200, [], true);
        return $response;
    }
}

The error :

Error on consol log

I was supposing that the problem was coming from cors but i’m using nelmio and that resolves the cors problem I encountered for another post request. the server is responding well to postman also. So if you know where the problem can be from please tell me

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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