Hello everyone,
Hello everyone,
I'm encountering an issue with the Cloudflare Worker KV where I get an empty array when trying to retrieve multiple keys at once.
When I run the following code, it returns an empty array:
However, this alternative approach works as expected:
According to the documentation, passing an array of keys to the
get()
method should return a Map
of the key-value pairs.
Here is my wrangler.jsonc
file for reference:
I've also made sure that the preview_id
is correctly configured for use with wrangler dev
.
Has anyone else experienced this issue or has any idea what might be going wrong?
Thanks2 Replies
.entries() is a command on Object but the return value of .get(keys) is a Map
this code works:
export default {
async fetch(request, env, ctx) {
// write a key-value pair
await env.KV.put('KEY', 'VALUE');
await env.KV.put('KEY2', 'VALUE2');
const value1 = await env.KV.get('KEY');
const value2 = await env.KV.get('KEY2');
const values = await env.KV.get(['KEY', 'KEY2'])
// return a Workers response
return new Response(
JSON.stringify({
values: Object.fromEntries(values),
value1: value1,
value2: value2
}),
);
}
}
I know why my code is broken
As you can see in my code, I used numeric keys ("2929", "2930")
If I try to add a non-numeric character to the key, the code works fine
Code:
Result:
Although I can simply use the above method, I would like to ask if there is a way for me to use a numeric key?