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

593
Visualizações
Uncaught TypeError: (intermediate value)(intermediate value)(intermediate value).map is not a function

I am building a React app that is requesting data from an an Api.here i am fecting data from api and i want to display it on a table. I am using ReactJS in frontend and NodeJS in backend.

here is my code.

import React, { useState, useEffect } from "react";
    import '../all.css';
    import Axios from "axios";

    function SearchProduct() {
        const [products, setProducts] = useState([]);


        const fetchProducts = async () => {
          const { data } = await Axios.get(
             "http://localhost:8080/api/QueryProductById/1"
        
            );
        
            let parseData = JSON.parse(data.response)

        
            setProducts(parseData);
        
        };
        
        const display = () => {
        
          return (products ?? []).map(product => (
            <tr key={product.id}>
               <th>{product.id}</th>
               <th>{product.name}</th>
               <th>{product.area}</th>
               <th>{product.ownerName}</th>
               <th>{product.cost}</th>
             </tr>
           ) );
          
         }
        useEffect(() => {
          fetchProducts();
        }, []);
        
        
          return (
            
            <div>
        
        <table>
          <thead>
            <tr>
              <th>ID</th>
              <th>Name</th>
              <th>Area</th>
              <th>Owner Name</th>
              <th>Cost</th>
        
            </tr>
          </thead>
          <tbody>
          {display()}
        
          </tbody>
          
         
        </table>
            </div>
          
          
          )
        }
        
export default SearchProduct;

i am getting uncaught error.

typeof data.response //is string


typeof products //is object

I have done almost every solution available but still can't resolve my error.

screenshot of error i am getting

screenshot of data in the API

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

try this code:

import React, { useState, useEffect } from "react";
import "../all.css";
import Axios from "axios";

function SearchProduct() {
  const [products, setProducts] = useState([]);

  const fetchProducts = async () => {
    const { data } = await Axios.get(
      "http://localhost:8080/api/QueryProductById/1"
    );

    let parseData = JSON.parse(data.response);

    setProducts(parseData);
  };

  useEffect(() => {
    fetchProducts();
  }, []);
//console.log(products)
  return (
    <div>
      <table>
        <thead>
          <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Area</th>
            <th>Owner Name</th>
            <th>Cost</th>
          </tr>
        </thead>
        <tbody>
          {products?.map((product) => (
            <tr key={product.id}>
              <th>{product.id}</th>
              <th>{product.name}</th>
              <th>{product.area}</th>
              <th>{product.ownerName}</th>
              <th>{product.cost}</th>
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  );
}

export default SearchProduct;
about 4 years ago · Santiago Trujillo 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