0

I'm working on react native project, and i'm calling API and extracting data from it , the data is succesfully extracted but I want to take arguments from the extracted data and I did'nt know how This is the code for fetching api :

Axios({
url: '/Authentification',
method: 'get',
baseURL: 'http://smart.netrostercloud.com/api',
transformRequest: [
  function (data, headers) {
    return data;
  },
],
transformResponse: [
  function (data) {
    // console.log(data);
    setData3(data);
  },
],
headers: {
  Authorization: 'Basic UxvwJc1GWjkOCyZoIHGuCD05gDUB72sqrgK30FgILho=',
},


});

  console.log(data3);

I've used data3 to extract the data in my main function so it can be visible. This is the data : enter image description here

I want to take CompanyCode,UserName,Password,FirstName and LastName

2 Answers 2

1

Firstly : axios its n asynchronous function, so its return a promise, Actually i dont prefere use this patern in simple api call l, but its good way if you know this concept and use oop like create instance of axios to reusable and cancel connectiin when component will unmount.

Fot this i prefere use this way
Const getUserData= async () =>{
try {
Const {data} = await axios.get(baseurl+route, headers , options) 
data & & SetData({username:data. User. Username,........... } )
Dosomthing... 
}
Catch(error) {
Dosomthing... 
}

Sign up to request clarification or add additional context in comments.

Comments

0

Before you storing data into data3 you need to stringify the data like this setData3(JSON.stringify(data));

and then try this:

if (data3) {
  const COMP_NAME = data?.user?.CompanyCode;
  const USER_NAME = data?.user?.UserName;
  CONST FIRST_NAME = data?.user?.FirstName;
  CONST LAST_NAME = data?.user?.LastName;
}

6 Comments

it render me an error : ERROR TypeError: undefined is not an object (evaluating 'data3.user.CompanyCode')
updated my answer please do a console of data3 and data3.user if any error occurs
see i'm putting this :const COMP_NAME = data3.user.CompanyCode; const USER_NAME = data3.user.UserName; console.log(USER_NAME); I had same error: TypeError: undefined is not an object (evaluating 'data3.user.CompanyCode')
Make sure data3 contains come values.. I have updated my answer please check
yeah it contains , I'll do this code after Axios ... right ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.