Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
¿Por qué no puedo acceder a las propiedades de mi respuesta JSON?

Estoy tratando de hacer una aplicación Spring Boot con comunicación websocket a través de lobby con ID. Cuando hago una solicitud de publicación para hacer un lobby, devuelve una cadena con una ID de juego (esto funciona bien). Realmente no soy una gran estrella en la interfaz, pero hice un juego y generé y recuperé la identificación. Luego, quiero unirme a un lobby aleatorio y recuperar el objeto Game al que el jugador se une aleatoriamente. Cuando recupero esos datos, no puedo acceder a sus propiedades de ninguna manera. ¿Alguien sabe por qué?

Código JavaScript:

 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); }); })(); }

Código Java:

 @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))); }

El resultado que obtengo de console.log() en la interfaz: console.log() interfaz

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Bueno, tienes un error tipográfico :) estás intentando consola.log(content.gameID); pero su clave es "gameId" con una "d" pequeña como se ve en el resultado de JSON.stringify(content);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!