I asked this question earlier and it got associated with this question ( How do I return the response from an asynchronous call? ) But It doesn't resolve my problem
i am very new to node.js and i'm having some error that i can't deal with or find any help that i understand. I used express-generator to initialize the app. I'm trying to send and object i get through an api call to the front end. I wrote a class "StockClass.js" with a function makeApiCall() which makes the call and returns the object. But when i call that function in the router i get "undefined". Heres the code
//==== StockClass.js ===
const yahooFinance = require("yahoo-finance");
class stockClass {
    static makeApiCall(symbol) {
        yahooFinance.quote(
            {
                symbols: [symbol],
                modules: ["price", "summaryDetail"],
            },
            function (err, quotes) {
                if (err) { console.log(err) }
                console.log(quotes)
                return quotes;
            }
        );
    }
}
module.exports = stockClass;
//=====index.js======
const StockClass = require("../handlers/StockClass");
router.get("/new", function (req, res) {
   let quotes = StockClass.makeApiCall("AAPL");
   console.log(quotes);
   res.render('path', { quotes });
});
The console.log in the StockClass.js logs the object (quotes) correctly while the console.log in index.js logs "undefined".
Link below explains the yahoo-finance api call. https://www.npmjs.com/package/yahoo-finance
==========================================================================
I also tried using a middleware and attaching the data to the response object like this
//==========middleware=========
const yahooFinance = require("yahoo-finance");
module.exports = {
    makeApiCall: (symbol) => {
        return function (req, res, next) {
            yahooFinance.quote(
                {
                    symbols: [symbol],
                    modules: ["price", "summaryDetail"],
                },
                function (err, quotes) {
                    if (err) { console.log(err) }
                    res.stockObj = quotes;
                    console.log(res.stockObj);
                }
            );
            next();
        }
    }
}
//======= index.js =========
const handler = require("./handlers/stockUtils");
    router.get("/new", handler.makeApiCall("AAPL"), function (req, res) {
       let quotes = res.stockObj;
       console.log(quotes);
       res.render('path', { quotes });
    });
And the results are the same. The console.log in the middleware function logs the correct object, but the console.log in index.js logs undefined
 
    