0

I have API key and I want fetch data, I have function to get data, but I get undefined value.

My code:

let url = "https://baza-gai.com.ua/nomer/KA0007XB";
let key = "2da3036efb196e460f0de7c55aa7295b";
let request = fetch(url, {headers: {"Accept": "application/json", "X-Api-Key": key}}).then(r => r.json());

let data; request.then(d => data = d);

console.log(data)

How I can get JSON from this url, and key?

Alireza Ahmadi
  • 8,579
  • 5
  • 15
  • 42
Andrew
  • 572
  • 6
  • 29

1 Answers1

0

The console.log executed before request completion. Try this one:

let data; request.then(d => {data = d; console.log(data)});
Alireza Ahmadi
  • 8,579
  • 5
  • 15
  • 42