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

181
Visualizações
React JS Axios API Calling Setting Data using Usestate not working

I am begginer in react JS. I am calling Api to get information of logged in User and storing some information in variable.

In API calling function the value that is taken from API is working Fine but outside that function it is showing undefined.

// getting info of logged in users;
  let parent_org;
  const userDetails = async () => {
    const user = await axios.get(
      "http://localhost:5000/api/v1/organization/me",
      {
        headers: {
          "x-access-token": await cookies.get("ivshr"),
        },
      }
    );
    parent_org = user.data.Data[0].org_id;
    console.log("Parent_org: "+parent_org);
    setParentOrg(user.data.Data[0].org_id);
    console.log("Parent Org of useState:"+parentOrg);
    console.log(user);
  };
  useEffect(() => {
    userDetails();
  }, []);



Above code is giving following output


Parent_org: 25
Parent Org of useState:undefined

I Tried to re use parent_org varibale while posting data via API Call but in that API call parent_org is showing also undefined

 const postSchema = async () => {
    try {
      console.log("Inside post");
      console.log("org_id: " + parent_org);
      const URL = "http://localhost:5000/api/v1/payroll/create-scheme";
      const response = await axios.post(URL, {
        scheme_name: Schema,
        org_id: parent_org,
      });
      console.log(response);
      if (response.data.Success === "Y") alert("Created Succesfully");
      else alert(response.data.Message);
    } catch (error) {
      console.log(error);
    }
  };


OUTPUT

Inside post
org_id: undefined

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