-2

Here is my React component:

const ChartItem = ({ id, apiUrl }) => {
    const [nivoChartData, setNivoChartData] = useState([]);

    //

    //declare the API call
    const apiCallAndConversionForNivoFormat = useCallback(async () => {
        try {
            const response = await axios.get(apiUrl);
            console.log("response: ");
            console.log(response);

            //converting api response into the format needed by nivo charts
            const dataConvertedForNivo = [
                {
                    id: response.data.symbol,
                    color: "hsl(90, 70%, 50%)",
                    data: response.data.historical.forEach((key) => {
                        key["x"] = key["date"];
                        key["y"] = key["close"];
                        delete key["date"];
                        delete key["close"];
                    }),
                },
            ];
            console.log("dataConvertedForNivo: ");
            console.log(dataConvertedForNivo);

            setNivoChartData(dataConvertedForNivo);
        } catch (e) {
            console.error(e);
        }
    }, [setNivoChartData, apiUrl]);

    useEffect(() => {
        apiCallAndConversionForNivoFormat();
    }, [apiCallAndConversionForNivoFormat]);

    return (
        <div className="chart-item">
            <NivoLineChart key={id} nivoData={nivoChartData} />
        </div>
    );
};

The logs: enter image description here

enter image description here

The original API response format before modifying:

{
    "symbol": "AAPL",
    "historical": [
        {
            "date": "2021-02-24",
            "close": 125.349998
        },
        {
            "date": "2021-02-23",
            "close": 125.860001
        },
        {
            "date": "2021-02-22",
            "close": 126
        },
    ]
}

^ I believe the response log is now showing "x" and "y" instead of "date" and "close" due to memoization.

nivo chart example format that I need to convert to:

[
  {
    "id": "AAPL",
    "color": "hsl(90, 70%, 50%)",
    "data": [
      {
        "x": "2021-02-24",
        "y": 125.349998
      },
      {
        "x": "2021-02-23",
        "y": 125.860001
      },
      {
        "x": "2021-02-22",
        "y": 126
      },
    ]
  }
]

Can anyone understand why the dataConvertedForNivo.data is undefined? And is there any better way to do this response conversion?

  • 3
    I guess you want `.map` instead of `.forEach`. If you find that `data` is blank, the first step is to [look up `forEach`](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) and check what it returns (nothing) –  Feb 25 '21 at 20:17
  • The response log shows that it is not blank, right? – jakobarsement Feb 25 '21 at 20:26
  • Yeah, I know, but when you construct your state array, you're populating your `data:` with `response.data.historical.forEach(...)`, which is, like I said, `undefined`. Like I said, you'll want to replace `.forEach` with `.map` -> problem solved. –  Feb 25 '21 at 21:41
  • (When I said data was blank, I was referring to `nivoChartData[0].data`, not `response.data`) –  Feb 25 '21 at 21:47

1 Answers1

1

You need to replace forEach with map.

const dataConvertedForNivo = [
    {
        id: response.data.symbol,
        color: "hsl(90, 70%, 50%)",
        data: response.data.historical.map(key => {
            key["x"] = key["date"];
            key["y"] = key["close"];
            delete key["date"];
            delete key["close"];
            return key;
        }),
    },
];