I'm using Axios to get a DeezerAPI, so fist of all I render info using an .map() and everything was fine when accessing information and passing it to a Component, but when I try to access a single JSON object I get an 'undefined' error.
Sample of Axios request React.js
//State with Redux
//No problem with Redux
const apiData = useSelector((state) => state.data);
//Local State to store fetched data from API
const [Songs, setSongs] = useState([]);
//AXIOS everything is fine here
useEffect(() => {
const getFetchData = async () => {
try {
const res = await axios.request({
method: "GET",
url: "https://deezerdevs-deezer.p.rapidapi.com/search",
params: { q: apiData }, //apiData is for searching an artist, song...
headers: {
"X-RapidAPI-Host": "deezerdevs-deezer.p.rapidapi.com",
"X-RapidAPI-Key": "randomNumbersKey",
},
});
const data = res.data.data;
setSongs(data); //stored in state
} catch (error) {
console.log(error);
}
};
getFetchData();
}, [apiData]);
So when I try to access a single JSON object using a console.log() as the following example
console.log(Songs[0].title)
I get an error saying this:
Uncaught TypeError: Cannot read properties of undefined (reading 'title')
I still don't know why this happen when selecting a single object, if I use a .map() I won't have any problem.
Thanks in advance for any help :D