I am facing an issue with my app that runs Rails in the backend and Javascript in the frontend. The controllers, routes, and CORS are all set up correctly. Both my Post and Get requests work without any problems. However, when I attempt to make a patch request, it successfully patches the data but returns an empty string when I use response.text()
. When trying to parse it using response.json()
, I encounter an error:
Uncaught (in promise) SyntaxError: Unexpected end of JSON input at game.js:25
. I need assistance in identifying the root cause of this problem.
static patchGame() {
const numberOfClicks = parseInt(document.getElementById('click-number').textContent, 10)
const score = parseInt(document.getElementById('score').textContent,10)
const gameID = document.getElementById('gameID').value
const gameObj = {game: {click_number: numberOfClicks, score: score}}
const options = {
method: "PATCH",
headers: {"Content-Type": "application/json",
"Accept": "application/json"},
body: JSON.stringify(gameObj)
}
fetch(`http://localhost:3000/games/${gameID}`, options).then(resp => {debugger}).then(game => { debugger }) // NEVER HITS last debugger
}
Below are the values from the resp
debugger:
>resp
<-Response {type: "cors", url: "http://localhost:3000/games/28", redirected: false, status: 204, ok: true, …}
body: ReadableStream
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 204
statusText: "No Content"
type: "cors"
url: "http://localhost:3000/games/28"
__proto__: Response
>resp.text()
<-Promise {<pending>}
__proto__: Promise
[[PromiseState]]: "pending"
[[PromiseResult]]: undefined
>resp.json()
<-Promise {<rejected>: TypeError: Failed to execute 'json' on 'Response': body stream already read
at eval (eval at <a…}
__proto__: Promise
[[PromiseState]]: "rejected"
[[PromiseResult]]: TypeError: Failed to execute 'json' on 'Response': body stream already read at eval (eval at <anonymous> (file:///Users/muratogulcansahin/Desktop/DanceMemory/frontend/game.js:1:1), <anonymous>:1:6) at file:///Users/muratogulcansahin/Desktop/DanceMemory/frontend/game.js:24:84
message: "Failed to execute 'json' on 'Response': body stream already read"
stack: "TypeError: Failed to execute 'json' on 'Response': body stream already read\n at eval (eval at <anonymous> (file:///Users/muratogulcansahin/Desktop/DanceMemory/frontend/game.js:1:1), <anonymous>:1:6)\n at file:///Users/muratogulcansahin/Desktop/DanceMemory/frontend/game.js:24:84"
__proto__: Error
>resp.body
<-ReadableStream {locked: true}
locked: true
__proto__: ReadableStream