Getting "The response is not writable." error
See original GitHub issueThis is my first time using Oak, and I’m running into an error that is making me scratch my head quite a bit. I can provide more code if need but it seems isolated to this chunk. I’m getting this error:
error: Uncaught Error: The response is not writable.
throw new Error("The response is not writable.");
^
at Response.set body (https://deno.land/x/oak/response.ts:120:13)
at file:///C:/Users/TARS/projects/deno-elixir/src/app.ts:31:24
but I don’t even have to adjust my response to get it to go away. I can just comment out the line above setting the response and it works. Returns “random string” in the body like expected.:
.get('/games', async (context) => {
// const gamesResponse = await API.get("/games");
context.response.body = "random string";
})
But if I uncomment that line back into the code, don’t change the response body and keep it as that random string, it crashes with that error. This errors out:
.get('/games', async (context) => {
const gamesResponse = await API.get("/games");
context.response.body = "random string";
})
I have no idea what could be causing that line to be affecting the next line, but the API.get request is fulfilled, I get a 200 and the data I’m expecting to store in the variable gamesResponse, but something about context.response.body doesn’t like that line.
Apologies if the problem is obvious, I’m still a junior dev and this is my first dive into Deno and using this package.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:11
- Comments:19 (5 by maintainers)
I had the same problem on routes that used a JWT auth middleware, my solution was to append await before my next() call, so instead of having:
I changed it to:
It works fine, and no errors 😉
Apparently, for me the problem is that I didn’t return a Promise in the middleware, my code was something like this:
However, since I’m not return a promise, this line was immediately invoked even before
doSomething
finished, which will in turn turn the response unwritable.So, by returning a promise as follows, I don’t face this problem anymore.