samedi 28 août 2021

If else displaying wrong answer when using fetch [closed]

So what I'm trying to do is create an if/else statement, where if your IP is from Japan the console log will show "You are in Japan", and if your IP is from any other country the console log will show "I guess you are in the USA". The only problem is, it doesn't work. Even when I use VPN to pretend I am from Japan, the console log still displays "I guess you are in the USA".

This is the code I currently have:

async function country() {
    let response = await fetch("https://ipinfo.io/json?token=8a21f471f4e583")
    let CountryResponse = await response.json()
    console.log(CountryResponse)

    if (CountryResponse == "JP") {
       console.log('You are in Japan');
    } else {
        console.log('I guess you are in the USA');
    }
  }

  country()

So basically I would just like for the console log to show "You are in Japan" if the IP is from Japan, anything else the console log can show "I guess you are in the USA".

Says I'm from the USA even tho IP is from Japan

Aucun commentaire:

Enregistrer un commentaire