1

I'm trying to get a value that is stored in the browser (application) in case the api is not able to find that stock name. I get undefined if I console log it from where I called it. If I console log it inside the function it gives me the correct value.

This is where I call the function called getStockPrice:

function getStockApi(stock, r, inputFinalValue) {
    fetch(`https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=${stock}&apikey=PPPUXKVI0RPUC2YM`)
        .then(function (res) {
            return res.json();
        })
        .then(function (data) {
            if (data['Global Quote']['05. price'] == null) {
                table.rows[r].cells[1].innerHTML = Store.getStockPrice(table.rows[r].cells[0].innerHTML);

            }

Here is the function itself:

class Store {


static getStockPrice(name) {
            const stocks = Store.getStock();
            stocks.forEach(stock => {
                if (stock.name == name && stock.price) {
                    return stock.price;
                }
            });
        }

static getStock() {
            let stocks;
            if (localStorage.getItem('stocks') === null) {
                stocks = [];
            } else {
                stocks = JSON.parse(localStorage.getItem('stocks'));
            }
            return stocks;
        }
}

0 Answers0