Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
Why can't i access the properties of my JSON response?

Im trying to make a Spring boot app with websocket communication through lobby's with ID's. When I make a post request to make a lobby it returns a String with a game ID (this works fine). I'm really not that much of a star on the frontend but I got making a game and generating, and retrieving the ID working. Then I want to join a random lobby and retrieve the Game object which the player joins randomly. When I retrieve that data I cannot access it's properties through any way. Anyone know why?

Javascript code:

async function startGame() {
 (async () => {
   const rawResponse = await fetch('http://localhost:8080/game/newgame', {
     method: 'POST',
     headers: {
       'Accept': 'application/json',
       'Content-Type': 'application/json'
     },
     body: JSON.stringify({name: 'Gerrt'})
   });
   const content = await rawResponse.json();

   console.log(content);

   stompClient.send("/drinkordice/topic/" + content);
        stompClient.subscribe('/topic/' + content, function (greeting) {
                   showGreeting(JSON.parse(greeting.body).content);
               });
 })();
}

function joinlobby(){
(async () => {
   const rawResponse = await fetch('http://localhost:8080/game/joinrandom', {
     method: 'POST',
     headers: {
       'Accept': 'application/json',
       'Content-Type': 'application/json'
     },
     body: JSON.stringify({name: 'Henk-Jan'})
   });
   const content = await rawResponse.json();

   console.log(content + " " + content['gameID'] + " " + content.gameID + " " + JSON.stringify(content));

   stompClient.send("/drinkordice/topic/" + content.gameID);
        stompClient.subscribe('/topic/' + content.gameID, function (greeting) {
                   showGreeting(JSON.parse(greeting.body).content);
               });
 })();
}

Java code:

@RestController
@RequestMapping("/game")
public class DrinkOrDiceController {

    Logger LOG = LoggerFactory.getLogger(DrinkOrDiceController.class);

    @Autowired
    DrinkOrDiceService drinkOrDiceService;

    public DrinkOrDiceController(){

    }

    @PostMapping("/newgame")
    public ResponseEntity<String> startNewGame(@RequestBody String playername) throws Exception {
        LOG.info("Request to make a new game by {}", playername);
        return ResponseEntity.ok((drinkOrDiceService.createGame(playername)));
    }

    @PostMapping("/joinrandom")
    public ResponseEntity<Game> joinRandomGame(@RequestBody String playername) throws Exception {
        LOG.info("Request to join a random game by {}", playername);
        return ResponseEntity.ok((drinkOrDiceService.joinRandomGame(playername)));
    }

The output I get from console.log() on the frontend: console.log() frontend

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Well, you have a typo:) you are trying to console.log(content.gameID); but your key is "gameId" with a small "d" as seen in the result of JSON.stringify(content);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda