Newbie help for using a value that appears in the console (npm)

I recently installed an external npm called consulta-dolar-venezuela (just in case, here is the read-me)

bcv2

It’s getting very difficult for me to find what to do with that, but I know I need that npm (in short: it gets the cost in my currency of the cost of the USD, since in my country there is hyperinflation, the price changes every moment).

I don’t know if a file has to be created in the backend or similar, but if I write the example code on a normal frontend page, in the console it gives me all the results from different sources, but if I clean a little the search gives me exactly the value I’m looking for:

const { getMonitor } = require("consulta-dolar-venezuela");

getMonitor("bcv", "price").then($ => { console.log($) })  //36.12

How can I use that value for an element in the page? I have made several attempts but ended up in dead ends, if I add more code to try to use that value, the line gives me the correct value in the console for today which is 36.12

But the text element page doesn’t show it, instead on the web it says NaN and in the console it says Promise<>, no idea why, please help me?

const { getMonitor } = require("consulta-dolar-venezuela");

getMonitor("bcv", "price").then($ => { console.log($) }) //= result;

let dolarForText = getMonitor("bcv", "price")

console.log(dolarForText); // console says Promise<> and not the price

const planA = 6

const resultPlanA = (planA * dolarForText).toFixed(2);

$w('#yesText').text = resultPlanA.toString(); //text says NaN

This function returns a Promise so we’ll need to handle it like one with the .then() syntax and set the text element to the result. You can learn more about how Promises work in JavaScript here: Promise - JavaScript | MDN

const { getMonitor } = require("consulta-dolar-venezuela");

getMonitor("bcv", "price").then((result) => {
    $w('#yesText').text = result;
})

This is a general JavaScript question which we don’t often answer so another way to find fixes for such issues is to try Googling them, for example: javascript returning promise instead of value - Google Search

1 Like

I didn’t know, my apologies, something I do before asking a question here is to search and read a lot of information to be sure that it is something that has not been answered before, something like “my last resort”, and even Thus, I omitted the obvious that these are promises from which I will learn, and although, as you say, it is something that they do not usually respond to, here you are, as a hero without a cape, thank you very much, I greatly value your help.

1 Like

Happy to help and no worries. Was just offering advice. Good luck on your journey to learn Javascript and feel free to ask Velo questions here :slight_smile: