0

Thanks in advance for the help I am just learning for fun.

I have the following code i have written:

async function fetchRSI(date, tickr) {
    try {
        //console.log("trying....");
        const response = await fetch('https://api.polygon.io/v1/indicators/rsi/' + tickr + '?timestamp=' + date + '&timespan=day&adjusted=true&window=14&series_type=close&order=desc&apiKey=3t1MkRpyjZYfpTl6Yd1x7LXQIFX9iKqr', {
            method: 'GET',
            credentials: 'same-origin'
        });
        const exam = await response.json();
        //console.log(exam);
        return exam
    } catch (error) {
        console.error(error);
    }
}

called via

RSI = await fetchRSI(formattedDate, tickr);
        console.log(RSI);

This gives me an output

{
  results: {
    underlying: {
      url: 'https://api.polygon.io/v2/aggs/ticker/AAPL/range/1/day/1640926800000/1647388800000?limit=75&sort=desc'
    },
    values: [ [Object] ]
  },
  status: 'OK',
  request_id: 'c799a17224ada3de6036840068ffdbe0'
}

if called via

RSI = await fetchRSI(formattedDate, tickr);
        console.log(RSI.results.values);

the output is

[ { timestamp: 1645074000000, value: 46.44735154180954 } ]

i am trying to access the value in this output however when i run:

RSI = await fetchRSI(formattedDate, tickr);
        console.log(RSI.results.values.value);

or
RSI = await fetchRSI(formattedDate, tickr);
        console.log(RSI.results.values[1]);

or
RSI = await fetchRSI(formattedDate, tickr);
        console.log(RSI.results.values['value']);

all return undefined

I have tried many more methods of accessing all return undefined. preferably i would like to save the value to a variable.

there has to be something Im missing?

Thanks,

  • `values: [ [Object] ]` is than an array of objects inside another array? if the key from that object is value then it should be `RSI.results.values[0].value[0]` – Chris G Apr 27 '23 at 15:11
  • Should be `RSI.results.values[0].value` because `values` is an array, and it has one element, at index 0, and that element is an object with a `value` property. Nothing out of the ordinary? – trincot Apr 27 '23 at 15:12
  • chris g-I think so? Im not 100% sure on the data type which is the issue i guess. I didn't want to list all the ways I tried to access it. I can send you the full code with the API key if you want to dig further. – budthespud Apr 27 '23 at 15:15
  • trincot - i have tried RSI.results.values[0].value this also returns undefined. – budthespud Apr 27 '23 at 15:16
  • `RSI.results.values[0][0].value` – Chris G Apr 27 '23 at 15:18
  • RSI.results.values[0].value[0] also gives a typeerror calling the result undefined – budthespud Apr 27 '23 at 15:34
  • RSI.results.values[0][0].value also giving TypeError: Cannot read properties of undefined (reading '0') – budthespud Apr 27 '23 at 15:36
  • Chris G - here is the solution i found, apparently it is a graph data type. ``` RSI = await fetchRSI(formattedDate, tickr); const get_keys = RSI.results.values; if (get_keys != null){ for (let i = 0; i < get_keys.length; i++) { let todayRSI = get_keys[i].value; console.log(todayRSI); } } else{ console.log('market closed!') } ``` – budthespud Apr 27 '23 at 17:14

0 Answers0