Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!