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

199
Visualizações
react await async calling too soon giving undefined then the right result

I'm using async and await, however the result it calling two sets of times, the first set returns the empty default from the useEffect, and then 2nd time returns the correct data from the call.

This is leading to the data not rendering in the component.

import { Grid } from '@material-ui/core';
import React, { useEffect, useState } from 'react';
import { useEasybase } from 'easybase-react';

export default function Data() {

    const [easybaseData, setEasybaseData] = useState([]);

    const { db } = useEasybase();

    const mounted = async() => {
        const ebData = await db("cars").return().limit(1).all();
        setEasybaseData(ebData);
    };

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

    console.log('easybaseData');

    const ez = easybaseData[0]; 

    const i = ez?.image;
    const n = ez?.name;
    const p = ez?.price;

    console.log(i);

    return (
        <Grid item xs={6} id={"outputs-grid"}>
            <div className="image-result-wrapper">
                <img
                    src={`http://localhost:3000/assets/images/${i}`}
                    className="image-result"
                />
                </div>
                <div className="details-result-wrapper">
                <p className="details-result">{n}</p>
                </div>
                <div className="price-result-wrapper">
                <p className="price-result">{`Evaluation: $${p}`}</p>
            </div>
        </Grid>
    )
}

and the console.log:

enter image description here

What am I missing about this? All 3 data for image, name and price are returning undefined in inspector.

enter image description here

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