I think I am missunderstanding how the async await is working, especially with a function from another file. So I want to get an adress from longitude and latitude using an axios call on an API
import { getAddressFromLongLat } from "../hooks/queries-hook";
import React, { useEffect, useState } from "react";
const Component = (props) => {
const [address, setAddress] = useState(null);
useEffect(() => {
const fetchFromLongLat = async () => {
if (props.coords) {
const response = await getAddressFromLongLat(props.coords);
console.log("response from nominatim " + JSON.stringify(response, null, 4));
setAddress(response);
}
};
fetchFromLongLat();
}, []);
return <></>;
};
then into queries-hook.js
import axios from "axios";
export const getAddressFromLongLat = async (coords) => {
const URL = "http://nominatim.openstreetmap.org/reverse";
const response = await axios.get(URL, {
params: {
format: "json",
lat: coords.lat,
lon: coords.long
},
headers: {
"Content-Type": "application/json; charset=UTF-8"
}
});
return {
street: response.address.road + " " + response.address.house_number,
cp: response.address.postcode,
city: response.address.town,
country: response.address.country_code.toUpperCase()
};
};
can someone explain me how I should write correctly the code with those async await to not have the error:
Possible Unhandled Promise Rejection (id: 0): TypeError: undefined is not an object (evaluating 'response.address.road')