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
}