#help

8 messages · Page 1 of 1 (latest)

hard burrow
#

hello!! im stuck on a js thing and i really don t understand why i cant get the data from this map function. it is always undefined.. thank you if someone can help
const getMyList = () => {

      fav.map(async(id) => {
        try{
          const {data:res} = await axios.get(url)
          return res
          
          console.log(Arr)
        } 
        catch(error) {
          console.log(error)
        } 
          
        })
        
        setNewArr([...newArr, res])
  
  }
  useEffect(() => {
      getMyList()
    },[]);
vale oriole
#

I'm not sure if I know enough to help you fix the problem completely but here are some issues that I see:
-"fav.map" would return a new array, not alter the fav array so if you want to use the result of the map call then you have to save the resulting array in a variable
-I don't think you'd be able to "res" outside of the .map call, I think it would be undefined because it isn't being defined in that scope. You'd probably have to use the resulting array and access the res somehow through that (e.g. array[index])
-Also, I'm not as familiar with async/await as I am using fetch but you'd have to somehow wait for data to come back from the axios call (for every call in the array that's generated by map) before you could call setNewArr(), otherwise you'll get promises for data instead of the data responses themselves

Without more code it's hard to know how to fix it so it works completely but hopefully some of this info helped.

mystic torrent
#

You should post more code, the repo or a scrimba link so we can check the whole code and understand a little bit more. 😉

hard burrow
#

Thank you, here Is the repo if you want to take a look

dusty loom
#

@hard burrow I've just loaded up your project on my local machine and you're getting a 401 error from the API call