Unable to access D1 database in a Nuxt 3 API route when deployed to Pages

What is the name of the domain?

n/a

What is the issue you’re encountering

Unable to access D1 database in a Nuxt 3 API route when deployed to Pages

What steps have you taken to resolve the issue?

Project is built in Nuxt 3 and is being deployed to Pages via GitHub integration.

Cloudflare Dashboard was used to create a binding in the Pages project to the D1 database using variable “DB”.

I have been unable to access this binding in an API route when the app is deployed.

‘Database connection is not configured.’ is the error received - which makes me think the binding isn’t properly being accessed. I haven’t been able to find consistent documentation on the chain of environment variables one must use in this context (accessing a D1 binding in Nuxt on Pages).

What are the steps to reproduce the issue?

In the API route (/server/api/database.js):

export default defineEventHandler(async (event) => {
try {

// access the database
const db = event.context.cloudflare.env.DB;

if (!db) {
  throw new Error('Database connection is not configured.');
}

This was user error. Cloudflare has updated the UI for its Pages settings - the environment (production/preview) dropdown scrolls out of view when accessing the binding setting. My binding was working correctly, it had just never been established for the preview environment.

This topic was automatically closed after 15 days. New replies are no longer allowed.