0

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

Sanjay Kumar
  • 55
  • 1
  • 9

0 Answers0