question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting "The response is not writable." error

See original GitHub issue

This 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:open
  • Created 3 years ago
  • Reactions:11
  • Comments:19 (5 by maintainers)

github_iconTop GitHub Comments

13reactions
Nick-Pashkovcommented, Jun 16, 2020

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:

if(token && csrfHeader) {
    if((await validateJwt(token, key)).isValid && validateCSRF(csrfCookie, csrfHeader)) {
       next()
    }

...

}

I changed it to:

if(token && csrfHeader) {
    if((await validateJwt(token, key)).isValid && validateCSRF(csrfCookie, csrfHeader)) {
       await next()
    }

...

}

It works fine, and no errors 😉

9reactions
wongjiahaucommented, Jun 8, 2020

Apparently, for me the problem is that I didn’t return a Promise in the middleware, my code was something like this:

app.use((ctx, next) => {
  doSomething((result) => {
    ctx.response.body.result
    next()
  })
})

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.

app.use((ctx, next) => {
  return new Promise(resolve => {
    doSomething((result) => {
      ctx.response.body.result
      resolve()
    })
  })
  .then(next)
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Deno oak compatible types for request and response
I have been playing with Deno Oak. Looked at some of the basic routing examples and none of them are using types for...
Read more >
Q: Error message: specified folder is not writable
When I try to save a file or edited photo from within an application to a folder on an external harddisk, I get...
Read more >
Stream was not writable error when accessing response stream
I have following code (C#) for reading contents from a given url: private string GetData(string url) { //Initialization HttpWebRequest ...
Read more >
Stream was not writable. - MSDN - Microsoft
Getting Started with ASP.NET. Getting Started with ASP. ... but I raised the error - "Stream was not writable." ... Close(); //Response.
Read more >
Files will not import "Not Writable" Error, can someone help?
correct answers 1 Correct answer ... You need to use your operating system features to check the properties of the destination folder to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found