uscjake87
uscjake87
CDCloudflare Developers
Created by uscjake87 on 2/13/2025 in #workers-help
Worker KV Not Binding
I converted a worker from UI to use github/local to push the code. That sync process is working fine to deploy it. The issue is the KV bindings are not holding. If I manually add the KV in the UI then it works, but on next deployment it wipes out the bindings in the UI. My toml:
name = "auth"
account_id = "123"
workers_dev = true
compatibility_date = "2025-02-10"
compatibility_flags = [ "nodejs_compat" ]

main = "./index.js"

[tail_consumers]
service = "tail"

[observability.logs]
enabled = true

kv_namespaces = [
{ binding = "CURATED_EMAIL", id = "abc", preview_id = "abc" }
]
name = "auth"
account_id = "123"
workers_dev = true
compatibility_date = "2025-02-10"
compatibility_flags = [ "nodejs_compat" ]

main = "./index.js"

[tail_consumers]
service = "tail"

[observability.logs]
enabled = true

kv_namespaces = [
{ binding = "CURATED_EMAIL", id = "abc", preview_id = "abc" }
]
I noticed when testing locally with wrangler dev it doesnt pickup these bindings either. I have setup my worker to handle multiple routes:
//index.js
import { registration } from './registration.js';

export default {
async fetch(request, env) {
const url = new URL(request.url);

if (url.pathname === '/registration') {
return registration(request, env);
}

return new Response('Not Found', { status: 404 });
},
};
//index.js
import { registration } from './registration.js';

export default {
async fetch(request, env) {
const url = new URL(request.url);

if (url.pathname === '/registration') {
return registration(request, env);
}

return new Response('Not Found', { status: 404 });
},
};
And route:
export async function registration(request, env) {
// Convert env object to JSON-safe format
// Changed
const envBindings = {};
for (const key in env) {
envBindings[key] = typeof env[key]; // Show the type of each binding
}

console.log('Available Environment Bindings:', JSON.stringify(envBindings, null, 2));

// Explicitly check if CURATED_EMAIL exists
if (!env.CURATED_EMAIL) {
console.error('ERROR: `CURATED_EMAIL` KV binding is missing in `env`!');
} else {
console.log('CURATED_EMAIL KV binding exists!');
}
export async function registration(request, env) {
// Convert env object to JSON-safe format
// Changed
const envBindings = {};
for (const key in env) {
envBindings[key] = typeof env[key]; // Show the type of each binding
}

console.log('Available Environment Bindings:', JSON.stringify(envBindings, null, 2));

// Explicitly check if CURATED_EMAIL exists
if (!env.CURATED_EMAIL) {
console.error('ERROR: `CURATED_EMAIL` KV binding is missing in `env`!');
} else {
console.log('CURATED_EMAIL KV binding exists!');
}
Everything works fine if I manually add the kv in the UI under worker settings. But I cant get the toml to deploy and actually work. Wipes it out every deployment 😦 Anybody got a clue?
11 replies