Access a KV entry in Pages with wrangler

trying to make a simple call in Pages and access a KV using wrangler local dev.

have this in my functions/test.ts:

export async function onRequest({env}) {
let kv = await env.mykv.get(“key1”);
return new Response(`Hello ${kv}.`);

only one line in wrangler.toml: (using x here to mask actual id’s)

kv_namespaces = [
{ binding = “mykv”, id = “xxxxxxxxxxxxxxxxxxxxxxxxxxxx41e0”, preview_id = “xxxxxxxxxxxxxxxxxxxxxxxxxxxx43af”}

then execute: wrangler pages dev . -k mykv

but all it prints in browser is: Hello null.

so seems like it can’t read the KV keys.

i can verify from Cloudflare dashboard that both KV namespaces exist, doublechecked the id’s and both contain key1 with a value.

any ideas?

1 Like

Same problem, did you resolve it?
BTW, I can get the value using command:

npx wrangler kv:key get --preview --binding=KV key1