Home > Software engineering >  TypeError: n.map is not a function
TypeError: n.map is not a function

Time:03-14

I am trying to return the data from this fetch in some cards in another component, but I get the following error:

TypeError: n.map is not a function.

I guess it's because of the async/await, but I don't know how to fix it.

Thanks a lot

export default function Container(){

    const [flights, getNewFlights] = useState({});

    const user = sessionStorage.getItem("username");
    const tipouser = sessionStorage.getItem("TipoUser");
    const APT = sessionStorage.getItem("Base");
    const Fecha = sessionStorage.getItem("Fecha");

    const fetchFlights = async () => {
        try {
          const flightsData = await $.ajax({
                url: "https://listVuelos.php",
                type: 'POST',
                data: {
                  APT,
                  Fecha
                }

            })
            getNewFlights(JSON.parse(flightsData))
        } catch (err) {
          console.log("Da error")
        }
      };

      useEffect(() => {
    
        fetchFlights()
    
        const interval = setInterval(() => {
          fetchFlights()
        }, 100000)
        return () => interval
    
      }, []);

    return(
      <Fragment>
      <div className="div_container">
         { flights?.map ( f => <IndexCards  data={f}></IndexCards> )}
      </div>
  </Fragment>
    );
}

CodePudding user response:

you can't use map function on an object to overcome the problem you can do something like this:

Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, newValue]))
  • Related