D
Deno

help

Uncaught (in promise) TypeError: cannot read headers: request closed

TMTimo Martinson6/11/2023
What is this? How to fix it?
SSorikairo6/11/2023
You might want to provide some code for people to be able to help you. The error itself is not quite useful, as it is very explicit. Request is closed, so you cannot read headers. You are probably sending a Response, then trying to read headers from the Request.
TMTimo Martinson6/11/2023
ahm, ok, sorry.
SSorikairo6/11/2023
The fact that it says "Uncaught (in promise)" hints that you might not be awaiting a promise
TMTimo Martinson6/11/2023
as the file is very small, here is it:
import { Application, Router } from 'https://deno.land/x/oak/mod.ts'
import { oakCors } from 'https://deno.land/x/cors/mod.ts'

import { City } from './types/city.ts'
import { Activity } from './types/activity.ts'

// --

const app = new Application()
const router = new Router()

// --

app.use(oakCors())

// --

const kv = await Deno.openKv('./src/database/kv')

// -- Städte --

router.post('/cities', async (context) => {
const id = crypto.randomUUID()
const data = await context.request.body().value
await kv.set(['cities', id], data)
const cityData = await kv.get(['cities', id])
const city: City = { id, ...cityData.value }
context.response.body = { city }
})

router.get('/cities', async (context) => {
const cities: City[] = []
for await (const cityData of kv.list({ prefix: ['cities'] })) {
const id = cityData.key[1]
const city: City = { id, ...cityData.value }
cities.push(city)
}
context.response.body = { cities }
})

// -- Aktivitäten --

router.post('/activities', async (context) => {
const id = crypto.randomUUID()
const data = await context.request.body().value
await kv.set(['activities', id], data)
const activityData = await kv.get(['activities', id])
const activity: Activity = { id, ...activityData.value }
context.response.body = { activity }
})

router.get('/activities', async (context) => {
const activities: Activity[] = []
for await (const activityData of kv.list({ prefix: ['activities'] })) {
const id = activityData.key[1]
const activity: Activity = { id, ...activityData.value }
activities.push(activity)
}
context.response.body = { activities }
})

// --

app.use(router.routes(), router.allowedMethods())

// --

app.listen({ port: 4000 })
import { Application, Router } from 'https://deno.land/x/oak/mod.ts'
import { oakCors } from 'https://deno.land/x/cors/mod.ts'

import { City } from './types/city.ts'
import { Activity } from './types/activity.ts'

// --

const app = new Application()
const router = new Router()

// --

app.use(oakCors())

// --

const kv = await Deno.openKv('./src/database/kv')

// -- Städte --

router.post('/cities', async (context) => {
const id = crypto.randomUUID()
const data = await context.request.body().value
await kv.set(['cities', id], data)
const cityData = await kv.get(['cities', id])
const city: City = { id, ...cityData.value }
context.response.body = { city }
})

router.get('/cities', async (context) => {
const cities: City[] = []
for await (const cityData of kv.list({ prefix: ['cities'] })) {
const id = cityData.key[1]
const city: City = { id, ...cityData.value }
cities.push(city)
}
context.response.body = { cities }
})

// -- Aktivitäten --

router.post('/activities', async (context) => {
const id = crypto.randomUUID()
const data = await context.request.body().value
await kv.set(['activities', id], data)
const activityData = await kv.get(['activities', id])
const activity: Activity = { id, ...activityData.value }
context.response.body = { activity }
})

router.get('/activities', async (context) => {
const activities: Activity[] = []
for await (const activityData of kv.list({ prefix: ['activities'] })) {
const id = activityData.key[1]
const activity: Activity = { id, ...activityData.value }
activities.push(activity)
}
context.response.body = { activities }
})

// --

app.use(router.routes(), router.allowedMethods())

// --

app.listen({ port: 4000 })
SSorikairo6/11/2023
which line throws the error ?
TMTimo Martinson6/11/2023
when the error occurs, both files (kv-shm and kv-wal) are deleted. i don't know the exact line it occurs while editing data on the client (react) seems it has something to do with KV !??!?! or deno run --watch ... as script ? i am guessing.
SSorikairo6/11/2023
So it fails when you do a POST request to one of the endpoint ? Error should have a stacktrace so you can deduct where the error come from, do not hesitate to post it here
TMTimo Martinson6/11/2023
i will, just waiting for it to occur again
SSorikairo6/11/2023
Oh, is it not happening every time ? Makes it quite hard 😭
TMTimo Martinson6/11/2023
yes it is. it is rarely happening. i think it might be a bug from early KV as i said. both database files are missing after the error
error: Uncaught (in promise) TypeError: cannot read headers: request closed
return this.#request.headers;
^
at Object.get headerList (ext:deno_fetch/23_request.js:115:17)
at Request.request.<computed> (ext:deno_fetch/23_request.js:565:60)
at Request.get [headers] (ext:deno_fetch/23_request.js:252:46)
at Request.get headers (ext:deno_fetch/23_request.js:452:16)
at NativeRequest.get headers (https://deno.land/x/oak@v11.1.0/http_server_native_request.ts:68:26)
at new Request (https://deno.land/x/oak@v11.1.0/request.ts:145:21)
at new Context (https://deno.land/x/oak@v11.1.0/context.ts:173:20)
at Application.#handleRequest (https://deno.land/x/oak@v11.1.0/application.ts:425:21)
at Application.listen (https://deno.land/x/oak@v11.1.0/application.ts:610:28)
at eventLoopTick (ext:core/01_core.js:182:11)
error: Uncaught (in promise) TypeError: cannot read headers: request closed
return this.#request.headers;
^
at Object.get headerList (ext:deno_fetch/23_request.js:115:17)
at Request.request.<computed> (ext:deno_fetch/23_request.js:565:60)
at Request.get [headers] (ext:deno_fetch/23_request.js:252:46)
at Request.get headers (ext:deno_fetch/23_request.js:452:16)
at NativeRequest.get headers (https://deno.land/x/oak@v11.1.0/http_server_native_request.ts:68:26)
at new Request (https://deno.land/x/oak@v11.1.0/request.ts:145:21)
at new Context (https://deno.land/x/oak@v11.1.0/context.ts:173:20)
at Application.#handleRequest (https://deno.land/x/oak@v11.1.0/application.ts:425:21)
at Application.listen (https://deno.land/x/oak@v11.1.0/application.ts:610:28)
at eventLoopTick (ext:core/01_core.js:182:11)
this is the full log maybe the problem is at the side of the react-client which breaks the connection ???
SSorikairo6/11/2023
Looks like an issue with oak, where the connection dies unexpectedly
TMTimo Martinson6/11/2023
ok
SSorikairo6/11/2023
Does the react client crashes or something ? Because requests ending unexpectedly when working locally seems pretty hard to do
TMTimo Martinson6/11/2023
yes.
TMTimo Martinson6/11/2023
Unhandled Rejection (TypeError): Failed to fetch
Unhandled Rejection (TypeError): Failed to fetch
TMTimo Martinson6/13/2023
I am working with parcel as a bundler. i am switching to vite. let's see if that works there seems to be a solution
UUUnknown User6/19/2023
Message Not Public
Sign In & Join Server To View
TMTimo Martinson6/25/2023
no, I did not find a solution.

Looking for more? Join the community!

Recommended Posts
Async OP that returns value instead of promiseIn `deno_runtime` is possible to have async op that returns the resolved value instead of promise? KFresh Logging MiddlewareHow would you go about writing a duration logger? Using 2 middlewares, I assumed they would behave VS-Code auto import local modulesHey is there a way to enable some sort of auto import for local modules/files in VS-Code? Currently In memory Deno KvHi there, I am exploring with [Deno Kv](https://deno.com/deploy/docs/kv) and it looks great! But myDeno Language Server Issue - Attempting to Use KV in Existing ProjectHi! I'm attempting to create a branch of an existing project (utilizing Fresh) and add KV as an expeTwitter APIIs there a easy way to fetch Tweets using Twitter API or something else? (since they put their API cIn browser video chat client & serverHello! How might people go about architecting an in-browser video chat client & server, specificallynpm:pusher working but http://esm.sh/pusher notAny ideas? deno run -A test.js //test.js import Pusher from "pusher" // const Pusher = require('pFile Permissions on Deno.FileInfoHello. As a programming exercise I'm trying to convert a Decimal number into Octal and that Octal reDeno Fresh - Different POST handlers for different <form>s ?what's the idiomatic way of handling different POST requests on the same page?Deno.open() questionDoes, when the process terminate, deno close the files if they were opened with `Deno.open()`?Can't figure out complicated type incompatibilityAnyone know what's going on here? `T` should have resolved to `[number, number, number]` https://wwManually edit existing KV tableHello! another day, another question. Is there a way to manually edit KV database to modify an entrLooking to deploy Deno Fresh on VPS using port 443Hello! I have two questions, I used saaskit as a base. However fresh's start function is using `serTrying to use Deno.serve() for an https static hostingI was trying to setup an simple hello world response in https on my custom domain."https://esm.sh/mercadopago@1.5.15" Is not workin on deploy"https://esm.sh/mercadopago@1.5.15" Is not workin on deno deploy, any clues? it works on local but Simulating physics collisions across isolatesA bit tangential to Deno, but since Deploy uses v8 isolates, it seems that a multiplayer online gameDeno showing node globalThis errorDeno is showing this error: ```js error: Uncaught ReferenceError: __DENO_NODE_GLOBAL_THIS_0_43_0__ iHow to read Chunked request body?Hello! Is it possible to read a chunked request body using `for await`? It seems to only read up to Deno Fresh (Saaskit base) - Impossible to debug errorWhen I try to access my account by logging in, the web app becomes unusable and I get this error