0

I'm sorry but I'm not being able to figure out what I'm missing here...

//calcular valor en usd
function usd_balance(variable) {
  //creamos un array para poner los precios
  var conversiones_usd = 123;
  //armamos el pedido
  req = 'https://blockchain.info/ticker';
  //revision
  console.log(req);
  request(req, { json: true }, (err, res, body) => {
    //en caso de error de comunicación
    if (err) { return console.log(err); }
    //revisamos el feedback
    //console.log(body);
    //variable de precio en usd
    conversiones_usd = body.USD.sell;
    //conversiones.eur = body.EUR.sell;
    //conversiones.gbp = body.GBP.sell;
    });
  //calculamos el valor
  console.log(conversiones_usd);
  var usd = (variable * conversiones_usd);
  return usd;
}

Trying to update the price with the request information, but it sticks to "123".

Does anyone have any clue? I'm sure enough I'm missing something obvious.

Thanks in advance dear community! Chris


Solved:

For the sake of anyone reading this: I updated this issue by reading the information using fs instead, fs seems to come by default in node, and reads informamation from a file. So this is what I'm doing:

//calcular valor en usd
function usd_balance(variable) {
  //creamos un array para poner los precios
  var conversiones = JSON.parse(fs.readFileSync('conversiones', 'utf8'));
  //pasamos a usd
  conversiones_usd = conversiones.USD.sell;
  conversiones_eur = conversiones.EUR.sell;
  conversiones_gbp = conversiones.GBP.sell;
  //armamos el pedido
  //req = 'https://blockchain.info/ticker';
  //revision
  //console.log(req);
  //request(req, { json: true }, (err, res, body) => {
    //en caso de error de comunicación
    //if (err) { return console.log(err); }
    //revisamos el feedback
    //console.log(body);
    //variable de precio en usd
    //conversiones_usd = body.USD.sell;
    //conversiones.eur = body.EUR.sell;
    //conversiones.gbp = body.GBP.sell;
    //});
  macro = (variable / 1000000);
  //calculamos el valor
  //informar conversiones
  //console.log(conversiones_usd);
  var usd = (macro * conversiones_usd);
  return usd;
}

And of course, the file with my Json data:

{
  "USD" : {"15m" : 6443.61, "last" : 6443.61, "buy" : 6443.61, "sell" : 6443.61, "symbol" : "$"},
  "AUD" : {"15m" : 8918.37, "last" : 8918.37, "buy" : 8918.37, "sell" : 8918.37, "symbol" : "$"},
  "BRL" : {"15m" : 25266.52, "last" : 25266.52, "buy" : 25266.52, "sell" : 25266.52, "symbol" : "R$"},
  "CAD" : {"15m" : 8486.12, "last" : 8486.12, "buy" : 8486.12, "sell" : 8486.12, "symbol" : "$"},
  "CHF" : {"15m" : 6406.74, "last" : 6406.74, "buy" : 6406.74, "sell" : 6406.74, "symbol" : "CHF"},
  "CLP" : {"15m" : 4302400.76, "last" : 4302400.76, "buy" : 4302400.76, "sell" : 4302400.76, "symbol" : "$"},
  "CNY" : {"15m" : 44682.59, "last" : 44682.59, "buy" : 44682.59, "sell" : 44682.59, "symbol" : "¥"},
  "DKK" : {"15m" : 42436.12, "last" : 42436.12, "buy" : 42436.12, "sell" : 42436.12, "symbol" : "kr"},
  "EUR" : {"15m" : 5692.19, "last" : 5692.19, "buy" : 5692.19, "sell" : 5692.19, "symbol" : "€"},
  "GBP" : {"15m" : 5076.53, "last" : 5076.53, "buy" : 5076.53, "sell" : 5076.53, "symbol" : "£"},
  "HKD" : {"15m" : 50582.69, "last" : 50582.69, "buy" : 50582.69, "sell" : 50582.69, "symbol" : "$"},
  "INR" : {"15m" : 454377.5, "last" : 454377.5, "buy" : 454377.5, "sell" : 454377.5, "symbol" : "₹"},
  "ISK" : {"15m" : 705768.49, "last" : 705768.49, "buy" : 705768.49, "sell" : 705768.49, "symbol" : "kr"},
  "JPY" : {"15m" : 713173.73, "last" : 713173.73, "buy" : 713173.73, "sell" : 713173.73, "symbol" : "¥"},
  "KRW" : {"15m" : 7327806.19, "last" : 7327806.19, "buy" : 7327806.19, "sell" : 7327806.19, "symbol" : "₩"},
  "NZD" : {"15m" : 9833.08, "last" : 9833.08, "buy" : 9833.08, "sell" : 9833.08, "symbol" : "$"},
  "PLN" : {"15m" : 24676.91, "last" : 24676.91, "buy" : 24676.91, "sell" : 24676.91, "symbol" : "zł"},
  "RUB" : {"15m" : 435858.91, "last" : 435858.91, "buy" : 435858.91, "sell" : 435858.91, "symbol" : "RUB"},
  "SEK" : {"15m" : 59467.84, "last" : 59467.84, "buy" : 59467.84, "sell" : 59467.84, "symbol" : "kr"},
  "SGD" : {"15m" : 8900.81, "last" : 8900.81, "buy" : 8900.81, "sell" : 8900.81, "symbol" : "$"},
  "THB" : {"15m" : 215023.38, "last" : 215023.38, "buy" : 215023.38, "sell" : 215023.38, "symbol" : "฿"},
  "TWD" : {"15m" : 198937.9, "last" : 198937.9, "buy" : 198937.9, "sell" : 198937.9, "symbol" : "NT$"}
}
Chris Russo
  • 450
  • 1
  • 7
  • 21

1 Answers1

0

This happens because every request in javascript is asynchronous, returning a promise, that is, its console.log is executed before the result is assigned to conversions_usd

you can use async / await, from ES7 or use promise. Give a researched on these subjects, there are different ways to apply.

Henrique Viana
  • 645
  • 4
  • 12