In my ClientData.js
I am using useEffect to call an API(which is working), then using useState to set the API response.data to a variable(which is working).
The response.data is an array of objects which I then set equal to a local variable. But when I attempt to access the object parameter I get a object undefined error.
I think the problem might be is that the object is undefined due to it waiting for the API response.
This is what the object from the API looks like:
objTest={
ClientDatabase: "21",
ClientID: "21",
ClientName: "21",
ClientServer: "21",
Country: "US - 21",
DatabaseVersion: "21",
Namespace: "21",
};
function SimpleSelect() {
const classes = useStyles();
const [objTest, setobjTest] = useState([]);
const clientAPI = useCallback( async() => {
//returns an array of objects
await axios({
method:'get',
url,
auth: {
username: user,
password: pass
}
})
.then(function(response) {
setobjTest( response.data)
})
.catch(function (error){
console.log(error);
});
})
useEffect( () => {
clientAPI();
}, [])
When I attempt to access the object it works using console.log((objTest[0]));
but when I attempt to access the objects parameter
like this: console.log((objTest[0].ClientDatabase));
it returns
TypeError: Cannot read property 'ClientDatabase' of undefined
This is the console log of response.data enter image description here
The response.data is an array.