-3

I am trying to save the json response to the jsondata variable for use in other functions. But it is saying it is undefined.

let jsondata;

fetch(
  `https://yahoo-finance15.p.rapidapi.com/api/yahoo/op/option/AMD`,
  {
    method: 'GET',
    headers: {
      'x-rapidapi-host': 'yahoo-finance15.p.rapidapi.com',
      'x-rapidapi-key': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    },
  }
)
  .then(
    function (u) { return u.json(); }
  ).then(
    function (json) {
      jsondata = json;
    }
  )
    
console.log(jsondata)
BNC Stocks
  • 53
  • 2
  • 8

1 Answers1

-1

I think the first parameter returned from the fetch promise is the response data So

`let jsondata;

fetch(

 `https://yahoo-finance15.p.rapidapi.com/api/yahoo/op/option/AMD`,
  {
    method: 'GET',
    headers: {
      'x-rapidapi-host': 'yahoo-finance15.p.rapidapi.com',
      'x-rapidapi-key': 
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    },
  }
)
  .then(
    function (u) { jsondata = u }
  )`
myestery
  • 105
  • 1
  • 11