NodeJS:变量未更新

时间:2018-08-15 15:48:55

标签: javascript node.js socket.io request

对不起,但我无法弄清这里我想念的是什么...

//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;
}

尝试使用请求信息更新价格,但价格会坚持为“ 123”。

有人有任何线索吗?我确信我确实缺少明显的东西。

在此先感谢亲爱的社区! 克里斯


已解决:

为了让任何人都读这本书:我改用fs读取信息来更新此问题,fs似乎默认出现在node中,并从文件中读取信息。所以这就是我正在做的:

//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;
}

当然还有包含我的Json数据的文件:

{
  "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$"}
}

1 个答案:

答案 0 :(得分:0)

之所以会发生这种情况,是因为javascript中的每个请求都是异步的,返回一个诺言,即其console.log在结果分配给conversions_usd之前执行

您可以从ES7使用async / await或使用promise。对这些主题进行研究,可以采用不同的方法。