Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
Cannot destructure property 'picture' of 'profileData' as it is undefined

I was trying to destructure my ProfileData variable that holds an object. But, I have a problem to display the API data, so when it passes, my page breaks and sends me this error "Cannot destructure property 'picture' of 'profileData' as it is undefined.".I don't know the reason of the error message is the absence of the key "last_paper" in some objects but I don't know how to avoid this problem. I don't know where the problem is and I don't know how to avoid this problem.

function Profile() {
   const { id: queryId } = useParams();
   const [profileData, setProfileData] = useState({});
   useEffect(() => {
      fetch(`http://localhost:8000/freelance?id=${queryId}`)
         .then((response) => response.json())
         .then((jsonResponse) => {
            setProfileData(jsonResponse?.freelaneData);
         });
   }, [queryId]);

   const { picture, name, location, tjm, job, skills, available, id } = profileData;
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

It looks like freelaneData is not present in all responses.

Either normalise your database response to include an empty object anyhow, or fallback to an empty object like:

setProfileData(jsonResponse?.freelaneData || {});
about 4 years ago · Juan Pablo Isaza Relatório

0

If you don't want your site to crash when profileData is undefined . Just follow the below code :

function Profile() {
   const { id: queryId } = useParams();
   const [profileData, setProfileData] = useState({});
   useEffect(() => {
      fetch(`http://localhost:8000/freelance?id=${queryId}`)
         .then((response) => response.json())
         .then((jsonResponse) => {
            setProfileData(jsonResponse?.freelaneData);
         });
   }, [queryId]);

   const { picture, name, location, tjm, job, skills, available, id } = profileData || {};

In this case , picture , name or other properties which we are trying to de structure will be undefined .

about 4 years ago · Juan Pablo Isaza Relatório

0

I just fixed the problem I had misspelled freelaneData instead of freelanceData. Thank you for your help.

function Profile() {
   const { id: queryId } = useParams();
   const [profileData, setProfileData] = useState({});
   useEffect(() => {
      fetch(`http://localhost:8000/freelance?id=${queryId}`)
         .then((response) => response.json())
         .then((jsonResponse) => {
            setProfileData(jsonResponse?.freelanceData);
         });
   }, [queryId]);

   const { picture, name, location, tjm, job, skills, available, id } =
      profileData;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda