简体   繁体   中英

Getting prices with an npm package not working

Actually I think there is an even better way to get the prices using this npm (you can open it to see better how it works):

https://github.com/jaggedsoft/node-binance-api than using the "coin "variable I am trying to use but I don't know how it could be possible, if you guys could help me to figure out with a better idea or the best way to pull prices with this package using discord it would be awesome, I've been already some days sticked to it :/

What I need: To be able to replace "ticker.XRPBTC" (code below).

For example if I write ETH it should be changed from ticker.XRPBTC to ticker.ETHBTC

var coin = (message.content.toUpperCase()).slice(2) + "BTC";

binance.prices((error, ticker) => {

  console.log("Price of " + coin + ":", ticker.XRPBTC);
});

for that I made the variable coin, I thought I could just write ticker.coin but it does not work...

I have tried this:


As an example:

ticker.XRPBTC     - This code works, output: the actual price of the currency.

What I am trying:

var coin = XRPBTC 

console.log(ticker.coin)  - output: undefined 

btw. I am writting console.log to test it in the console but I also have an error if I write:


  if (msg.startsWith ("eth")) {
    message.reply ("Price of " + coin + ":", ticker.TRXBTC);
  }

an explanation of what ticker does:

The ticker has the function to get the last price of each currency, it can be for example ETHBTC, XRPBTC, TRXBTC etc. in this case eth, xrp and trx are the currencies. So in the ticker I just need to write it that way so that I can get the price for that pair.

As you can see in the code I sent you above, I am getting the text exactly how the ticker would "need it to look".

So if I write eth, the variable "coin" is transforming it into ETHBTC

Best!

I think you're looking for property access . You might want to check if the coin exists first, eg

var coin = message.content.toUpperCase().slice(2) + "BTC";
console.log(coin); // logs e.g. "ETHBTC"
binance.prices((error, ticker) => {
  if (coin in ticker) {
    var price = ticker[coin]; // <- property access
    message.reply("Price of " + coin + ": " + price);
  } else {
    // handle error, e.g.
    message.reply("Coin " + coin + " doesn't exist");
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM