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

180
Visualizações
React - How to parse the array while fetching?

I'm not getting the array from the express API.

In Express Code, It contains two Main Array - headArr & titleArr to Send as Response.


Express Code:

app.use(cors());
app.use(express.json());

app.post('/api/v1', (request, response) => {

  let headArr = [{ head: 'head 1' }, { head: 'head 1' }];
  let titleArr = [{ title: 'title 1'}, { title: 'title 1' }];

  const data = request.body;

  if (data.term == 'head') {
    response.send({ data: headArr }); //sending the headArr
  }

  else if (data.term == 'title') {
    response.send({ data: titleArr });  //sending the titleArr
  }
});

app.listen(8080);

ReactJS CODE:

function fetchData() {

  const sender = { "term": "title" }; //requesting the term title

  let result = await fetch(`http://localhost:8080/api/v1`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
    },
    body: JSON.stringify(sender)
  });

  let output = await result.json();
  console.log(output);  // Output: {data: Array(0)}. Not getting Array here.
}

useEffect(() => {
  fetchData();
}, []);

Console Output: FrontEnd

{data: Array(0)}  //I got the response. But not that array.

You can clearly see this here. I've got Complete JSON response from the server as {data: Array(0)}.

How Do I Get that head & title Array?

about 4 years ago · Juan Pablo Isaza
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