-
Notifications
You must be signed in to change notification settings - Fork 233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting "The response is not writable." error #148
Comments
I am afraid I can't guess at what The error occurs when the response has been processed, most likely something is calling |
It's just a library for cleaning up HTTP fetches to an API (https://api-v3.igdb.com/), here's exactly what it does in a fetch call instead:
That's what I thought too after looking at the files in the repo for where the error was occurring and seeing that's the only function that could make #writable false. Not sure what could be triggering that function though. |
Just in case you need it for reference here's the repo, the error is in src/app.ts:25:24 currently. |
this error also occurs when I call: await Deno.readFile .... |
In post route case, this problem is same.
It looks await timing issue. |
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 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)
}) |
it seems that the problem occurs because the function returns before the body is written, what I'm really confused by is that when using promises, oak awaits the promise and then disables writing but when using async/await, even though the function still returns a Promise, it is not awaited |
@GideonMax I think this PR #156 might help solve your problem, which is to allow Typescript to guide you from making these mistakes. Basically it works by making Typescript complaining when you're writing a middleware that will potentially cause Unfortunately it's being closed at the moment, hopefully I can convince the owner that the PR is worthwhile. |
I was in the same error for a week. |
Same error here. When I try to fetch asynchronously some data from my DB with mongo@v0.7.0, I get .post('/uploadFile/:projectId', upload('uploads'), async(context: any)=>{
// Do stuff
const project: Project | undefined = await projects.findOne({
_id: { "$oid": context.params.projectId}
})
// Do stuff
context.response.body = 'Any response'
}) If I remove this DB request, everything works fine, but this BTW, I found a way to make it work, moving this request to a middleware and passing the information I need through the context. const checkProject = async(context: any, next: any) => {
const project = await projects.findOne({_id: { $oid: context.params.projectId}})
if(project){
context.project = project
await next()
}
}
// ...
router
.post('/uploadFile/:projectId', checkProject, upload('uploads'), async(context: any)=>{
// Do stuff
const project: Project | undefined = context.project
// Do stuff
context.response.body = 'Any response'
}))
} |
I was having a similar issue and what worked for me was to add a return statement to a function on the route handler function. I haven't figured it out yet why, but here's the scenario: DOESN'T WORK: router.get(
"/user/:userId/device/:deviceId/active-time",
(ctx) => {
const paramsAndQuery = helpers.getQuery(ctx, { mergeParams: true });
getActiveTime(
{
response: ctx.response,
params: {
userId: paramsAndQuery.userId,
deviceId: paramsAndQuery.deviceId,
interval: paramsAndQuery.interval,
},
},
);
},
); DOES WORK: router.get(
"/user/:userId/device/:deviceId/active-time",
(ctx) => {
const paramsAndQuery = helpers.getQuery(ctx, { mergeParams: true });
return getActiveTime( /** <<<---- notice the `return` statement **/
{
response: ctx.response,
params: {
userId: paramsAndQuery.userId,
deviceId: paramsAndQuery.deviceId,
interval: paramsAndQuery.interval,
},
},
);
},
); The getActiveTime function: const getActiveTime = async (
{ params, response }: {
params: {
userId: string;
deviceId: string;
interval: string;
};
response: any;
},
) => {
try {
const netTime = await getUserDeviceNetActiveTime(
params.userId,
params.deviceId,
interval,
);
response.status = 200;
response.body = netTime;
} catch (error) {
console.error("Error fething getUserDeviceNetActiveTime:", error);
throw error;
}
} |
I have the similar problem before.
|
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 ;) |
I also replaced by --> this works fine in my case. after this the controller content can be something like:
|
Dear All, I have the similar problem when using some middleware. The problem exist inside async await function, where if await is missed before next(). Please don't forget to use await before next() or other method inside asycn function. Regards |
It happens to me when I try to change the JS object inside the route.
The response isn't writable error is thrown.
|
This is the response that solved it for me. I went hunting up the middleware chain, and found a |
I have create a repo with a small example of where I have been hitting this issue. |
Anybody got solution to this problem? I am having the same issue, when I try to read values with |
for others looking at this, check that if you are using cors, that you do it after your other middleware. this solved the problem for me. not working:
working:
hope this helps! |
The most common cause of this is "dropping" the flow control of the middleware. For simple middleware, dropping the flow control usually is not an issue, for import { Application, Router } from "https://deno.land/x/oak/mod.ts";
const router = new Router();
router.get("/", (ctx) => {
ctx.response.body = "hello world";
});
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(); A much better solution is to always be explicit about the flow control of the import { Application, Router } from "https://deno.land/x/oak/mod.ts";
const router = new Router();
router.get("/", (ctx, next) => {
ctx.response.body = "hello world";
return next();
});
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(); |
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:
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.:
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:
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.
The text was updated successfully, but these errors were encountered: