Whats wrong with my wrangler.toml file?

For some reason my D1 database isnt showing up. It does exist but the page function doesnt seem to know about it despite my wrangler.toml file defining it. wrangler.toml
name = "my_app"
compatibility_date = "2023-10-30"
pages_build_output_dir = "./dist"

[[d1_databases]]
binding = "DB"
database_name = "my_app_database"
database_id = "(guid)"
migrations_dir = "sql/migrations"

[vars]
STAGE = "local"

[env.preview.vars]
STAGE = "preview"

[env.production.vars]
STAGE = "production"
name = "my_app"
compatibility_date = "2023-10-30"
pages_build_output_dir = "./dist"

[[d1_databases]]
binding = "DB"
database_name = "my_app_database"
database_id = "(guid)"
migrations_dir = "sql/migrations"

[vars]
STAGE = "local"

[env.preview.vars]
STAGE = "preview"

[env.production.vars]
STAGE = "production"
Function code
import { D1Database } from "@cloudflare/workers-types";
import EventContext from "./EventContext";

class Database {
db: D1Database;

constructor(ctx: EventContext) {
if (!ctx.env.DB) {
console.error("Context environment:", ctx.env);
throw new Error("Database instance is not available in the environment context.");
}
this.db = ctx.env.DB;
}
...
import { D1Database } from "@cloudflare/workers-types";
import EventContext from "./EventContext";

class Database {
db: D1Database;

constructor(ctx: EventContext) {
if (!ctx.env.DB) {
console.error("Context environment:", ctx.env);
throw new Error("Database instance is not available in the environment context.");
}
this.db = ctx.env.DB;
}
...
Exception
"exceptions": [
{
"stack": " at new Database (functionsWorker-0.5557135572831049.js:230:13)\n at onRequestPost2 (functionsWorker-0.5557135572831049.js:388:20)\n at async next (functionsWorker-0.5557135572831049.js:905:26)\n at async Object.fetch (functionsWorker-0.5557135572831049.js:919:14)",
"name": "Error",
"message": "Database instance is not available in the environment context.",
"timestamp": 1717214376469
}
],
"exceptions": [
{
"stack": " at new Database (functionsWorker-0.5557135572831049.js:230:13)\n at onRequestPost2 (functionsWorker-0.5557135572831049.js:388:20)\n at async next (functionsWorker-0.5557135572831049.js:905:26)\n at async Object.fetch (functionsWorker-0.5557135572831049.js:919:14)",
"name": "Error",
"message": "Database instance is not available in the environment context.",
"timestamp": 1717214376469
}
],
Log
{
"message": [
"Context environment:",
{
"ASSETS": {},
"CF_PAGES": "1",
"CF_PAGES_BRANCH": "main",
"CF_PAGES_COMMIT_SHA": "(commit sha)",
"CF_PAGES_URL": "(generated page url)",
"STAGE": "production"
}
],
"level": "error",
"timestamp": 1717214376469
}
{
"message": [
"Context environment:",
{
"ASSETS": {},
"CF_PAGES": "1",
"CF_PAGES_BRANCH": "main",
"CF_PAGES_COMMIT_SHA": "(commit sha)",
"CF_PAGES_URL": "(generated page url)",
"STAGE": "production"
}
],
"level": "error",
"timestamp": 1717214376469
}
5 Replies
jordanfinners
jordanfinners6mo ago
I would try updating your compatibility date to a more recent one. My D1 has been working with a date of 2024-05-02
Register
RegisterOP6mo ago
I'll give that a shot. ty
Register
RegisterOP6mo ago
Well, I did that and it's still not working. Page Functions settings:
No description
Register
RegisterOP6mo ago
No description
Register
RegisterOP6mo ago
Here's my wrangler.toml again
name = "(app name)"
compatibility_date = "2024-05-02"
pages_build_output_dir = "./dist"

[[d1_databases]]
binding = "DB"
database_name = "(database name)"
database_id = "(D1 database ID)"
migrations_dir = "sql/migrations"

[vars]
STAGE = "local"

[env.preview.vars]
STAGE = "preview"

[env.production.vars]
STAGE = "production"
name = "(app name)"
compatibility_date = "2024-05-02"
pages_build_output_dir = "./dist"

[[d1_databases]]
binding = "DB"
database_name = "(database name)"
database_id = "(D1 database ID)"
migrations_dir = "sql/migrations"

[vars]
STAGE = "local"

[env.preview.vars]
STAGE = "preview"

[env.production.vars]
STAGE = "production"
@jordanfinners
Want results from more Discord servers?
Add your server