Home > Back-end >  Error retrieving data from api call as attribute is different
Error retrieving data from api call as attribute is different

Time:09-21

Hi there I'm using a api which returns as follows

{"secret-finance":{"usd":0.04883396}}

Problem is I'm using vue and retrieving data like this

async getCurrentSefiPrice() {
  await axios
    .get(
      "https://api.coingecko.com/api/v3/simple/price?ids=secret-finance&vs_currencies=usd"
    )
    .then(
      (response) =>
        (this.sefi_token_current_price = response.secret-finance.usd)
      // console.log(response)
    );
  console.log(this.sefi_token_current_price);
}

But when I use secret-finance to get usd value I get an error. Thanks in advance.

CodePudding user response:

@haseebsaeed You'll need to reference it as

response["secret-finance"].usd

You need to use key notation rather then dot notion when a key has a hyphen in it. Any key that contains characters that are not allowed in JavaScript variables you'll need to reference them as above.

A further example, If the secret-finance object has a property key of us-dollars rather than the current one of usd, you would then access it by doing,

response["secret-finance"]["us-dollars"]
  • Related