Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: countries.map is not a function #4

Open
E-Jorge opened this issue Apr 22, 2020 · 8 comments
Open

TypeError: countries.map is not a function #4

E-Jorge opened this issue Apr 22, 2020 · 8 comments

Comments

@E-Jorge
Copy link

E-Jorge commented Apr 22, 2020

First of all let me thank you, for sharing this valuable knowledge
I coded along with you and everything went well for the first couple of minutes. However in the end I´m getting the following error:

TypeError: countries.map is not a function
19 | return (
20 |
21 | <NativeSelect defaultValue="" onChange={(e) => handleCountrychange(e.target.value)}>

22 | Global
| ^ 23 | {countries.map((country, i) => {country})}
24 |
25 |

@kunalkashyap855
Copy link

The name of the variable here should be what you have written in useState. According to the video, the name of the state variable is 'fetchedCountries' instead of 'countries', that might be the problem.

@FottyM
Copy link

FottyM commented Apr 26, 2020

Can you share the entire code snippet?

@harshilparmar
Copy link

@FottyM Brother @kunalkashyap855 is saying that you should write like this
{fetchedCountries.map((country, i) => {country})}

@E-Jorge
Copy link
Author

E-Jorge commented Apr 26, 2020

I managed fix the problem with your support. Thank you!

@gellys
Copy link

gellys commented Jun 3, 2020

Hello guys, taking advantage of the fact that it's still open, I'm having a similar problem:

TypeError: fetchedCountries.map is not a function
CountryPicker
tracker/src/components/CountryPicker/CountryPicker.jsx:21
18 | return (
19 |
20 |

21 | Global
| ^ 22 | {fetchedCountries.map((country, i) => (
23 |
24 | {country}

Below is how my code is :

question-function

@FottyM
Copy link

FottyM commented Jun 4, 2020

Edit:
@gellys setFetchedCountries is expecting an array but I am guessing what it is getting is an object because you are passing await fetchCountries to setFetchedCountries what you could do is.

const countries = await fetchCountries()
setFetchedCountries(countries)

// or 
setFetchedCountries(await fetchCountries())

@harshilparmar
Copy link

@FottyM yes you are right, @gellys setFetchedCountries(await fetchCountries) is not calling method,so I think it is getting whole reference of fetchCountries method.

@gellys
Copy link

gellys commented Jun 4, 2020

Edit:
@gellys setFetchedCountries is expecting an array but I am guessing what it is getting is an object because you are passing await fetchCountries to setFetchedCountries what you could do is.

const countries = await fetchCountries()
setFetchedCountries(countries)

// or 
setFetchedCountries(await fetchCountries())

That's it! Now it worked... Thanks @FottyM 💯 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants