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
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"
did you double check your wrangler.toml values for the db?
i am shaking
thank you very much
do you see a database there?
i even got the api response
ah ok good!
whats your wrangler command to connect to the production database?
I copied it above
oh
i meant something like
I don't think you can
hm, thats a shame
This might sound simple, but did you update these values for the d1 database you want to bind?
yeah
that's my template and i was trying to find what was wrong with it, but thanks for checking in
š only saying because I've definitely not done that before :hide_the_pain: :laughingpepe:
One thing I can suggest, is try getting rid of the conditional regarding
process.env.NODE_ENV
, my nextjs pages project looks similar to this
With a wrangler file of
i tried it, and for some reason
@cloudflare/next-on-pages
throws an error, so I left the conditione
unrelated to this thread, but did you manage to view the preview or prod database using drizzle-kit studio
?
UPDATE: the issue is fixed with drizzle-kit v0.21.3, THANK YOU! ā¤ļø