I got this:
import React, { useState, useEffect } from 'react';
import axios from "axios";
function InsideRacesComponent() {
const route = "http://localhost:4000/api/standings";
const [data, setData] = useState([{ raceName: "", raceCountryFlag: "" }]);
useEffect(() => {
axios.get(route).then(res => {
//The arrays that I want to fetch data and combine
const raceNameData = res.data[0].InsideRaceResults
const raceCountryData = res.data[0].RaceResults
raceNameData.forEach(races => {
const raceName = races.gpId;
});
raceCountryData.forEach(races => {
const flagLink = races.gpCountryImage;
})
})
}, []);
return (
//some html
)
}
If I log raceNameData it looks like this:
(17) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {_id: "5fe8c55830013624e4716eed", gpId: "Austrian Grand Prix", grandPrixDate: "Sunday, July 5", grandPrixTrack: "Red Bull Ring", RaceResult: Array(20)}
1: {_id: "5fe8c55830013624e4716f02", gpId: "Styrian Grand Prix", grandPrixDate: "Sunday, July 12", grandPrixTrack: "Red Bull Ring", RaceResult: Array(20)}
2: {_id: "5fe8c55830013624e4716f17", gpId: "Hungarian Grand Prix", grandPrixDate: "Sunday, July 19", grandPrixTrack: "Hungaroring", RaceResult: Array(20)}
...
And raceCountryData like this:
(17) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {_id: "5fe8c55830013624e4716edc", gpCountryName: "Austria", gpCountryImage: "https://upload.wikimedia.org/wikipedia/commons/thu…lag_of_Austria.svg/1200px-Flag_of_Austria.svg.png"}
1: {_id: "5fe8c55830013624e4716edd", gpCountryName: "Austria", gpCountryImage: "https://upload.wikimedia.org/wikipedia/commons/thu…lag_of_Austria.svg/1200px-Flag_of_Austria.svg.png"}
2: {_id: "5fe8c55830013624e4716ede", gpCountryName: "Hungary", gpCountryImage: "https://upload.wikimedia.org/wikipedia/commons/thu…lag_of_Hungary.svg/1920px-Flag_of_Hungary.svg.png"}
...
Im looking for this when I log data from React Hook
console.log("data: " + data);
// data: [{ raceName: "Austrian Grand Prix", raceCountryFlag: "https://upload.wikimedia.org/wikipedia/commons/thu…lag_of_Austria.svg/1200px-Flag_of_Austria.svg.png" }, { raceName: "Styrian Grand Prix", raceCountryFlag: "https://upload.wikimedia.org/wikipedia/commons/thu…lag_of_Austria.svg/1200px-Flag_of_Austria.svg.png" } , { raceName: "Hungarian Grand Prix", raceCountryFlag: "https://upload.wikimedia.org/wikipedia/commons/thu…lag_of_Hungary.svg/1920px-Flag_of_Hungary.svg.png" }, ...]
I've tried with this in each one of the "array.forEach":
setData(prevValue => [...prevValue, { raceName: raceName, raceCountryFlag: "" }]
//and
setData(prevValue => [...prevValue, { raceName: "", raceCountryFlag: flagLink }]
of course my aproach has not logic 😅...
Thanks!
raceNameDataandraceCountryData. I assume that you're using MongoDB, so you're probably looking for something like this. And I advise you that you take care of that business logic on the back end, not the front end, if you're the one developing it.