-4

this function returns API response, I would like to get a specific field value, let's say "price" from this response, how do I do that?

export const getprice = async () => {
    return await axios.get(
        "https://api.pancakeswap.info/api/v2/tokens/0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82"
    );
};

function returns this:

{"updated_at":1620658369033,"data":{"name":"PancakeSwap Token","symbol":"Cake","price":"40.19439880437819571616287343907344","price_BNB":"0.05840738921546340105964944169352627"}

By the way, the API token is public.

Lin Du
  • 88,126
  • 95
  • 281
  • 483

1 Answers1

1
export const getPrice = async () => {
    let price = await requestData().then(response=>response.data['price'])
    return price
};
async function requestData(){
    try{
        let response = await axios.get("https://api.pancakeswap.info/api/v2/tokens/0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82")
        return response;
    }catch(error){
        //do something with error
    }
}

P.S. Don't show anyone private api tokens

Allure
  • 513
  • 2
  • 12
  • 4
    Mixing `await` and `then` is typically not a good idea, but i guess OP already used the `return await` anti-pattern as well, so whatever. – ASDFGerte May 10 '21 at 15:01