0

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

JEFF
  • 153
  • 2
  • 10

2 Answers2

1

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);

Pramod Jangam
  • 316
  • 3
  • 10
  • I need to call a node function – JEFF Sep 24 '20 at 19:55
  • 1
    Node is not a programing language. it is framework. The method you have written I believe it is in javascript, which can be run via browser or NodeJS. If you want to run this method via NodeJS, you will need to host it through a server. You can use vanilla NodeJS hosting using http module or you can use framework like express or restify to do so. Then you can make ajax call to your Nodejs server which may call this method internally. Reference: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Node_server_without_framework – Pramod Jangam Sep 25 '20 at 05:18
  • then create route and call that internal route via Ajax, right? – JEFF Sep 25 '20 at 06:24
  • 1
    Yes Exactly @JEFF – Pramod Jangam Sep 26 '20 at 06:16
0

using the solution given in the link . you can add the infromation in every page and then since it is added in the script, all the js function in the page will be able to access it.

deepak thomas
  • 1,118
  • 9
  • 23