How can I access `env.DB` in my Pages/Worker on production?
Steps to reproduce:
1. Clone https://github.com/cjxe/nextjs-d1-drizzle-cloudflare-pages
2. Follow
README.md
.
3. Run pnpm start
or pnpm pages:prod
.
4. Launch the web app using your favourite browser.
Error:
I don't get this error when I run on dev
due to how next.config.mjs
is set up, but I can't make this work on production. The main issue is the Cloudflare pages return an error when it tries running getRequestContext().env.DB
in src/server/db/index.ts
.GitHub
GitHub - cjxe/nextjs-d1-drizzle-cloudflare-pages
Contribute to cjxe/nextjs-d1-drizzle-cloudflare-pages development by creating an account on GitHub.
29 Replies
Thanks for the reply!
I made your suggested changes, and the terminal threw this error:
TypeError: Cannot read properties of undefined (reading 'prepare')
šµāš«what's calling the database? how is it imported?
- db/index.ts: https://github.com/cjxe/nextjs-d1-drizzle-cloudflare-pages/blob/main/src/server/db/index.ts
- server action where the db is called: https://github.com/cjxe/nextjs-d1-drizzle-cloudflare-pages/blob/main/src/server/functions/customers.ts
i can do a 5 minute demo on screen share if you have some time
can you try a simple api route like that?
the db is the one exported above
sure, 2 secs
how would you like me to fetch in the front end, navigate to the endpoint in the browser?
to test the api route you use something like postman and call GET localhost:3000/api/something
I use thunder client in vscode
![No description](https://cdn.answeroverflow.com/1242163214368768112/image.png)
i am getting the same error
both thunder client and my browser returned the same
are you exporting the db object as I showed you?
i mean the server returned the sam error, the browser and thunder client returned "500 internal server error"
![No description](https://cdn.answeroverflow.com/1242163434183983144/image.png)
did you double check your wrangler.toml values for the db?
i am shaking
thank you very much
do you see a database there?
![No description](https://cdn.answeroverflow.com/1242164351696375808/image.png)