I was trying to get the client IP address using ipify API. I have used fetch and await & I am getting the right response by calling the prototype. But while I am trying to return the value from the function and trying to store it on a const variable, it's showing undefined. I need the IP address value to pass on weather API. code:-
class CurrentLocaion{
//Fetch current IP from api ipfy
async getCurrentIpAddress(){
const response = await fetch('http://api.ipify.org/?format=json');
const responseData = await response.json();
return responseData;
}
}
//init currentlocation object
const currentLocation = new CurrentLocaion();
function getIpAddress(){
currentLocation.getCurrentIpAddress()
.then(results =>{
return results.ip;
})
}
const ipvalue = getIpAddress();
console.log(ipvalue);
I think I might have done some silly mistake, But unable to discover. Any help will be a big thank you.