Hyperdrive fails to connect in `scheduled()` (but works over HTTP)

The same worker either will or won't connect to my Hyperdrive DB depending on whether I invoke it over HTTP (will) or CRON( won't). I'm experiencing this while testing locally, with the --remote flag enabled. Specifically, I get:
DB ERROR TypeError: Cannot read properties of undefined (reading 'connectionString')
DB ERROR TypeError: Cannot read properties of undefined (reading 'connectionString')
In my DB handler, I put the following:
console.log(env.db?.connectionString)
console.log(env.db?.connectionString)
Over HTTP, I get:
postgresql://********:******…/**********?sslmode=disable
postgresql://********:******…/**********?sslmode=disable
Over CRON, I get (via the --test-scheduled flag and the /__scheduled endpoint):
undefined
undefined
Accordingly, DB queries work over HTTP but I get errors on CRON. Here's how I specify my DB in dev.toml:
hyperdrive = [
{ binding = "db", id = "5e1*************", localConnectionString = "postgresql://***************" }
]
hyperdrive = [
{ binding = "db", id = "5e1*************", localConnectionString = "postgresql://***************" }
]
Here's what runs when I run npm run dev:
wrangler dev --config "dev.toml" --remote --port 1933 --env dev --test-scheduled
wrangler dev --config "dev.toml" --remote --port 1933 --env dev --test-scheduled
Here's what then comes up (see image below). As you can see, my Hyperdrive DB is there. Any help appreciated
No description
4 Replies
Adrian
Adrian2w ago
Hey wrangler dev with hyperdrive to a remote db is a known not working feature currently. Is this the scenario you are attempting?
Mitya
MityaOP2w ago
Hi - could you possibly elaborate? This is the setup I've had for over a year and it works perfectly over HTTP? From the docs:
To access your production (remote) Hyperdrive configuration, pass the --remote flag
This is what I'm doing. My dev DB is remote, and is created as a HD, and I connect to it locally via --remote. Just to be clear, with the above setup:
export default {
fetch: (req, env) => console.log(env.db.connectionString), //exists
scheduled: (evt, env) => console.log(env.db.connectionString) //undefined
}
export default {
fetch: (req, env) => console.log(env.db.connectionString), //exists
scheduled: (evt, env) => console.log(env.db.connectionString) //undefined
}
Adrian
Adrian2w ago
Ah ok I misread that you were connecting over HTTP and not HTTPS. HTTPS scenario is what doesn't work currently. Hm ok I'll look at the --test-scheduled flag to see what is making it behave differently
Mitya
MityaOP2w ago
Thanks, really appreciate it. If it helps, I used to get this error every time I made code changes while testing schedules, i.e. the next time I visited the /__schedule endpoint after Wrangler's HMR had uploaded the changes. I'd then refresh, and it would work fine. But that first time, after code changes, I'd always get this. Now suddenly I get it constantly. There's definitely something fishy here; without any changes, everything just started working again i.e. the DB connected. I refresh, and it's back to the above error with CF saying it can't read the HD connection string. Refresh 10 more times, same error. On the 11th time, it works 🤷

Did you find this page helpful?