Please I'd like to save the json response to a variable, but it shows undefined when printing it.
let rawdata;
fetch("https://api.db-ip.com/v2/free/self", {
method: 'GET',
headers: {
Accept: 'application/json',
},
},
).then(response => {
if (response.ok) {
response.json().then(
data => rawdata = data).then(
() => console.log(rawdata)
)
};})
console.log(JSON.stringify(rawdata));
console.log(rawdata);
message = {
"ipAddress": rawdata.ipAddress,
"countryName": rawdata.countryName,
"city": rawdata.city,
"browser": browserName
}
The first log is correct, but the second one isn't.
Thanks in advance.