Trying to show total number of coins within the query but .total is not working in React

In my react project all is going good. In one of the sections I’m trying to show total number of coins but whenever I’m trying to get it by .total value it’s throwing an error… " Cannot read properties of undefined (reading ‘total’) "

1 Like

It is hard to see what is going on without some code. The error message you’re encountering, “Cannot read properties of undefined (reading ‘total’)”, typically indicates that the path you’re using to access the total property in your code is not correctly pointing to the nested structure of the JSON response from your API endpoint.

I suspect you are using this endpoint? Get a List of Coins - API Documentation | Coinranking, which returns the following:

{
  "status": "success",
  "data": {
    "stats": {
      "total": 3,
...more json...

To get to the total part you have to do something like the following:

const totalCoins = response.data.stats.total;

I hope that helps.

1 Like