Skip to content
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

Context storage not available #3944

Open
samducker opened this issue Feb 23, 2025 · 3 comments
Open

Context storage not available #3944

samducker opened this issue Feb 23, 2025 · 3 comments
Labels

Comments

@samducker
Copy link

What version of Hono are you using?

4.6.12

What runtime/platform is your app running on? (with version if possible)

Cloudflare Workers

What steps can reproduce the bug?

When implementing https://hono.dev/docs/middleware/builtin/context-storage#context-storage-middleware with https://developers.cloudflare.com/workers/runtime-apis/nodejs/asynclocalstorage/ and the correct flags listed in the docs I get the error

✘ [ERROR] A request to the Cloudflare API (/accounts/MY_CLOUDFLARE_ID/workers/scripts/d/versions) failed.

  Uncaught Error: Context is not available
    at getContext
  (file:///Users/sam/Documents/GitHub/d/s/node_modules/hono/dist/middleware/context-storage/index.js:12:11)
  in getContext
    at null.<anonymous>
  (file:///Users/sam/Documents/GitHub/d/s/apps/api/src/services/auth/index.ts:14:28)
   [code: 10021]

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

@yusukebe
Copy link
Member

Hi @samducker

I can't reproduce it. Can you share a minimal project to reproduce it?

@samducker
Copy link
Author

samducker commented Feb 23, 2025 via email

@hls-app
Copy link

hls-app commented Feb 25, 2025

@yusukebe

Using hono 4.7.2 and having similar issue

This works

import { drizzle } from 'drizzle-orm/neon-http'
import { getContext } from 'hono/context-storage'
import type { AppEnv } from '@/shared/app'

export const db = () => {
  const c = getContext<AppEnv>()
  return drizzle(c.env.DATABASE_URL)
}

But this does not

import { drizzle } from 'drizzle-orm/neon-http'
import { getContext } from 'hono/context-storage'
import type { AppEnv } from '@/shared/app'

const getDb = () => {
  const c = getContext<AppEnv>()
  return drizzle(c.env.DATABASE_URL)
}

export const db = getDb()

Error

 Uncaught Error: Context is not available

    at null.<anonymous> (index.js:8277:11) in getContext
    at null.<anonymous> (index.js:26059:13) in getDb
    at null.<anonymous> (index.js:26062:10)

Ideally, I want to use something like this:

import { drizzle } from 'drizzle-orm/neon-http'
import { getContext } from 'hono/context-storage'
import type { AppEnv } from '@/shared/app'

const getDatabaseUrl = (): string => {
  return getContext<AppEnv>().env.DATABASE_URL
}

export const db = drizzle(getDatabaseUrl())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants