Search code examples
javascriptnode.jspug

Call a node function on each page


I have a function to get a value from the database. I'd like to call that function for every page and retrieve its value. it's a node function like this:

async function getBitcoinPrice() {

    let btcprice = await cryptoPrice.findOne({"pair": 'BTC'});
    if(!btcprice){
        let price = await btcValue({currencyCode: 'EUR'});
        btcprice = await cryptoPrice.findOneAndUpdate({"pair": 'BTC'},{"pair":'BTC','price' : price},{upsert:true,new:true});
        return btcprice.price;
    }


    return btcprice;

}

I'd like to load the value on a header div. I'm using Pug Template


Solution

  • Probably you need to use $(document).ready if you are using jQuery. Or you can simply call this method in script tag in your layout page. You can also try window.attachEvent("onload", fn);