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
- Information: "Intraday (1min) open, high, low, close prices and volume"
- Symbol: "NVAX"
- Last Refreshed: "2023-08-31 19:59:00"
- Interval: "1min"
- Output Size: "Compact"
- 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?