withastro / adapters

Home for Astro's core maintained adapters
64 stars 33 forks source link

Astro.locals.runtime.env throws error @astro/cloudflare@10.2.4 #259

Closed Glowin closed 4 months ago

Glowin commented 4 months ago

Astro Info

Astro                    v4.8.2
Node                     v20.11.0
System                   macOS (arm64)
Package Manager          npm
Output                   server
Adapter                  @astrojs/cloudflare
Integrations             none

Describe the Bug

After running npm run dev, an error occurred when accessing the page, and the error message is as follows. However, after downgrading @astro/cloudflare to 9.1.0, it runs correctly.

An error occurred.
Cannot read properties of undefined (reading 'env')

pages/index.astro:6:41
---
import { desc } from "drizzle-orm";
import { drizzle } from "drizzle-orm/d1";
import { linkShare } from "../schema";

const db = drizzle(Astro.locals.runtime.env.DB);

const links = await db
    .select()
    .from(linkShare)
    .orderBy(desc(linkShare.created));
---

Stack Trace
TypeError: Cannot read properties of undefined (reading 'env')
    at /Users/jiangbian/Documents/projects/astro/cf-d1/src/pages/index.astro:6:41
    at index (/Users/jiangbian/Documents/projects/astro/cf-d1/node_modules/astro/dist/runtime/server/astro-component.js:16:12)
    at callComponentAsTemplateResultOrResponse (file:///Users/jiangbian/Documents/projects/astro/cf-d1/node_modules/astro/dist/runtime/server/render/astro/render.js:90:31)
    at renderToAsyncIterable (file:///Users/jiangbian/Documents/projects/astro/cf-d1/node_modules/astro/dist/runtime/server/render/astro/render.js:118:32)
    at renderPage (file:///Users/jiangbian/Documents/projects/astro/cf-d1/node_modules/astro/dist/runtime/server/render/page.js:31:30)
    at lastNext (file:///Users/jiangbian/Documents/projects/astro/cf-d1/node_modules/astro/dist/core/render-context.js:125:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async callMiddleware (file:///Users/jiangbian/Documents/projects/astro/cf-d1/node_modules/astro/dist/core/middleware/callMiddleware.js:21:10)
    at async RenderContext.render (file:///Users/jiangbian/Documents/projects/astro/cf-d1/node_modules/astro/dist/core/render-context.js:148:22)
    at async handleRoute (file:///Users/jiangbian/Documents/projects/astro/cf-d1/node_modules/astro/dist/vite-plugin-astro-server/route.js:209:18)

What's the expected result?

I hope the same code can produce the same results as version 9.1.0 of @astro/cloudflare.

Link to Minimal Reproducible Example

https://github.com/Glowin/cf-d1

Participation

alexanderniebuhr commented 4 months ago

@Glowin The adapter config inside astro.config.mjs changed. This is documented in docs and in the Upgrade Guide https://github.com/withastro/adapters/releases/tag/%40astrojs%2Fcloudflare%4010.0.0

You need to update the config, and everything should work. I would also suggest to add // @ts-check at the top of the file, to make sure the configuration is checked against those issues.