Workers KV get throws TypeError: Cannot read properties of undefined (reading 'get')

My wrangler.toml file looks like this:
kv_namespaces = [
{ binding = "BH_API_KEYS", id = "xxx" },
]
kv_namespaces = [
{ binding = "BH_API_KEYS", id = "xxx" },
]
In my index.ts file I have:
export interface Env {
BH_API_KEYS: KVNamespace;
}

...
export default {
async email(message: any, env: Env, ctx: ExecutionContext) {
const testApiKey = await env.BH_API_KEYS.get("testId");
...
export interface Env {
BH_API_KEYS: KVNamespace;
}

...
export default {
async email(message: any, env: Env, ctx: ExecutionContext) {
const testApiKey = await env.BH_API_KEYS.get("testId");
...
This code results in the error:
TypeError: Cannot read properties of undefined (reading 'get'
TypeError: Cannot read properties of undefined (reading 'get'
I have verified that my namespace with the given ID exists and is named like
worker-name_namespace-name
worker-name_namespace-name
. Why isn't my worker able to access my KV?
3 Replies
emilycheyne
emilycheyneOP10mo ago
If it's relevant I am using
wrangler 3.31.0
wrangler 3.31.0
kian
kian10mo ago
You probably have kv_namespaces under a TOML table so it’s not actually binding any namespaces
emilycheyne
emilycheyneOP10mo ago
hmm, i am not sure what you mean. This is what my entire wrangler.toml file looks like:
name = "vms-receiver-email-camect"
main = "src/index.ts"
compatibility_date = "2024-03-04"

# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
# https://dash.cloudflare.com/946e6bcae03c19645421637bc83c9232/workers/kv/namespaces/65a1763d5ec540ad9c46c8495f5623ae
kv_namespaces = [
{ binding = "BH_API_KEYS", id = "xxx" }
]
name = "vms-receiver-email-camect"
main = "src/index.ts"
compatibility_date = "2024-03-04"

# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
# https://dash.cloudflare.com/946e6bcae03c19645421637bc83c9232/workers/kv/namespaces/65a1763d5ec540ad9c46c8495f5623ae
kv_namespaces = [
{ binding = "BH_API_KEYS", id = "xxx" }
]
Ah, thanks for the hint! I changed the binding to: [[kv_namespaces]] binding = "BH_API_KEYS" id = "xxx" and everything is working as expected
Want results from more Discord servers?
Add your server