Access stock data and visualize it on a wix website

85 views Asked by At

I try to access stock prices and plot them on a wix website using java script.

$w.onReady(function () {
    const apiUrl = 'https://www.alphavantage.co/query? 
       function=TIME_SERIES_INTRADAY&symbol=NVAX&interval=1min&apikey=API_KEY';

    function getStockPrice() {
        fetch(apiUrl)
        .then(response => response.json())
        .then(data => {
            console.log(data)
            const stockData = data['Time Series (1min)']; 
            const lastRefreshedTime = data['Meta Data']['3. Last Refreshed'].replace('T', ' '); 
            const latestPrice = stockData[lastRefreshedTime]['4. close']; 
            
            $w("chart").text = `current price: ${latestPrice}`;
        })
        // .catch(error => {
        //     console.error('Failure to retrieve stock price data:', error);
        // });
    }

    function updateStockPrice() {
        getStockPrice(); 
        setTimeout(updateStockPrice, 60000); 
    }

    updateStockPrice();
});

The request seems to work as in the console appears data:

{...}jsonTableCopy JSON

Meta Data: {...}jsonTableCopy JSON

  1. Information: "Intraday (1min) open, high, low, close prices and volume"
  2. Symbol: "NVAX"
  3. Last Refreshed: "2023-08-31 19:59:00"
  4. Interval: "1min"
  5. Output Size: "Compact"
  6. Time Zone: "US/Eastern"

Time Series (1min): {...}

But every 60 seconds there appears an error message:

Error: Cannot read properties of undefined (reading '2023-08-31 19:59:00')

I don't understand the problem. Any Ideas?

0

There are 0 answers