I am busy learning javascript, and it, as well as react, nodejs and npm are all brand new concepts to me, so bear with me.
I have managed to piece together an application which returns the weather for my location by following this tutorial: https://www.freecodecamp.org/news/learn-react-by-building-a-weather-app/
I run npm and then I am able to view the executed app on localhost:3000/
When I make changes to the .js application (I use atom), npm compiles the new code and it works perfectly.
But when I reload the web page it doesn't work (returns nothing).
Looking at the console in Chrome, when I do a compile, it immediately gets my location and the API call to openweathermap works (it includes the location in the call of course).
But when I reload the page, it seems like the location is not successfully fetched, and then obviously it cannot return anything. What's weird is that it seems to attempt it 3 times, and the second time it works, but the first and third time it fails.
Here is the piece of code where the issue seems to lie and I also attached an image of what the app looks like when it WORKS, and what the console shows when it does NOT work (when reloading).
useEffect(() => {
const fetchData = async () => {
navigator.geolocation.getCurrentPosition(function(position) {
setLat(position.coords.latitude);
setLong(position.coords.longitude);
});
await fetch(`${process.env.REACT_APP_API_URL}/weather/?lat=${lat}&lon=${long}&units=metric&APPID=${process.env.REACT_APP_API_KEY}`)
.then(res => res.json())
.then(result => {
setData(result)
console.log(d) // (GL)
console.log("Latitude is:", lat) // (GL)
console.log("Longitude is:", long) // (GL)
console.log(result);
});
}
fetchData();
}, [lat,long])