I have a problem with React. When I want to access the properties of a model class in a Spring-Boot, React project then I get an error. I think that the component does nothing extraordinary and is relatively simple. I did not change anything in the Webpack settings.
The API returns the object, but I can't access the properties from within React. Whenever I do, I get the following error.
Object in Dev Tools
{id: "62ac8a1e5018474c3d349d0d", name: "Escape from Tarkov", date: "2016-12-28",…}
bewertungen: [{gewichtung: 5, comment: "Richtig gut."}, {gewichtung: 5, comment: "Super realistisch"}]
0: {gewichtung: 5, comment: "Richtig gut."}
1: {gewichtung: 5, comment: "Super realistisch"}
date: "2016-12-28"
entwickler: "Battlestate Games"
id: "62ac8a1e5018474c3d349d0d"
name: "Escape from Tarkov"
Error message
Module parse failed: Unexpected token (105:10)
You may need an appropriate loader to handle this file type.
| columnNumber: 21
| }
> }, game?.id))));
| }
My component:
import axios from "axios";
import React, { useEffect, useState } from "react";
import { Game } from "../types/Game";
export default function GameOverview() {
const [game, setGame] = useState<Game>();
const [isLoading, setIsLoading] = useState(true);
const GAME_TO_FETCH = "escape from tarkov";
const fetchGameData = async (url: string) => {
await axios
.get(url)
.then((response) => response.data)
.then((data) => {
console.log(data);
setGame(data);
setIsLoading(false);
})
.catch((error) => console.log("Something bad happend: " + error));
}
useEffect(() => {
fetchGameData("http://localhost:8080/games/" + GAME_TO_FETCH);
}, []);
if (isLoading)
return <div>Loading...</div>
return (
<>
<div>
<h3>Game - Details Page</h3>
<ol>
<li>Some Game data 1</li>
<li>Some Game data 2</li>
<li>{ game?.id }</li> {/* Throws the error */}
</ol>
</div>
</>
)
}
Model interface
export interface Game {
id: string;
name: string;
date: Date;
entwickler: string;
ratings: Rating[];
}
interface Rating {
gewichtung: number;
comment: string;
}
Thanks in advance. I hope someone can help me and I can learn something from the circumstance.