0

I try to retrieve data only on the body object, and I am confused by this section

My code

      coin_price: async function(callback){
        var coingecko = await CoinGeckoClient.simple.price({
                ids: config.tickercoins,
                vs_currencies: ['usd', 'btc', 'idr', 'eur', 'jpy', 'krw'],
              });
            callback(null, coingecko);
      },

Result:

"coin_price": {
"success": true,
"message": "OK",
"code": 200,
"data": {
  "loki-network": {
    "usd": 0.202249,
    "btc": 0.000055,
    "idr": 2905.52,
    "eur": 0.176356,
    "jpy": 22.18,
    "krw": 225.42
     }}},

And I want to be like this:

    "coin_price": {
    "usd": 0.202249,
    "btc": 0.000055,
    "idr": 2905.52,
    "eur": 0.176356,
    "jpy": 22.18,
    "krw": 225.42
    },

And I tried to edit my code like:

      coin_price: async function(callback){
        var coingecko = await CoinGeckoClient.simple.price({
                ids: config.tickercoins,
                vs_currencies: ['usd', 'btc', 'idr', 'eur', 'jpy', 'krw'],
              });
            callback(null, coingecko.data);
      },

And result:

     "coin_price": {
       "loki-network": {
       "usd": 0.202249,
       "btc": 0.000055,
       "idr": 2905.52,
       "eur": 0.176356,
       "jpy": 22.18,
       "krw": 225.42
       }},

The code: callback(null, coingecko.data[0]); does not work

Is there an example so the results can be as I want?

1
  • 1
    coingecko.data['loki-network'] Commented Jan 1, 2019 at 20:05

2 Answers 2

1

The coingecko.data object uses the key "loki-network", not 0. Adjust your callback so that it returns that property instead:

callback(null, coingecko.data["loki-network"]);
Sign up to request clarification or add additional context in comments.

Comments

0

Like @Mark Meyer said in the comments, in order to get the json like you want you can do this:

//this is the resposne data
let json = {"coin_price": {
"success": true,
"message": "OK",
"code": 200,
"data": {
  "loki-network": {
    "usd": 0.202249,
    "btc": 0.000055,
    "idr": 2905.52,
    "eur": 0.176356,
    "jpy": 22.18,
    "krw": 225.42
     }}}}
let coin_price = json.coin_price.data["loki-network"] //this is where you access the data
console.log(coin_price)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.