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:
const values = await options.env.TEST.get(["2930", "2929"]);
console.log([...values.entries()]); // []

// Expected: [["2930", "foo"],["2929","bar"]]
const values = await options.env.TEST.get(["2930", "2929"]);
console.log([...values.entries()]); // []

// Expected: [["2930", "foo"],["2929","bar"]]
However, this alternative approach works as expected:
const values = await Promise.all(["2930", "2929"].map((key) => options.env.TEST.get(key)));
console.log(values); // ["foo", "bar"]
const values = await Promise.all(["2930", "2929"].map((key) => options.env.TEST.get(key)));
console.log(values); // ["foo", "bar"]
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:
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "***",
"main": "./src/index.ts",
"compatibility_date": "2025-06-20",
"observability": {
"enabled": true
},
"assets": {
"directory": "./public/",
"binding": "ASSETS"
},
"kv_namespaces": [
{
"binding": "TEST",
"id": "***",
"preview_id": "***"
}
]
}
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "***",
"main": "./src/index.ts",
"compatibility_date": "2025-06-20",
"observability": {
"enabled": true
},
"assets": {
"directory": "./public/",
"binding": "ASSETS"
},
"kv_namespaces": [
{
"binding": "TEST",
"id": "***",
"preview_id": "***"
}
]
}
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? Thanks
2 Replies
thomasgauvin
thomasgauvin2mo ago
.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 }), ); } }
Hth4nh
Hth4nhOP5w ago
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:
app.get("/test_kv", async (c) => {
await Promise.all([c.env.KV.put("2929", "VALUE"), c.env.KV.put("2930", "VALUE2")]);

const value1 = await c.env.KV.get("2929");
const value2 = await c.env.KV.get("2930");
const values = await c.env.KV.get(["2929", "2930"]);

await Promise.all([c.env.KV.put("2929zzz", "VALUEzzz"), c.env.KV.put("2930zzz", "VALUE2zzz")]);

const value1zzz = await c.env.KV.get("2929zzz");
const value2zzz = await c.env.KV.get("2930zzz");
const valueszzz = await c.env.KV.get(["2929zzz", "2930zzz"]);

return new Response(
JSON.stringify(
{
values: Object.fromEntries(values),
value1: value1,
value2: value2,
valueszzz: Object.fromEntries(valueszzz),
value1zzz: value1zzz,
value2zzz: value2zzz,
},
null,
2,
),
);
});
app.get("/test_kv", async (c) => {
await Promise.all([c.env.KV.put("2929", "VALUE"), c.env.KV.put("2930", "VALUE2")]);

const value1 = await c.env.KV.get("2929");
const value2 = await c.env.KV.get("2930");
const values = await c.env.KV.get(["2929", "2930"]);

await Promise.all([c.env.KV.put("2929zzz", "VALUEzzz"), c.env.KV.put("2930zzz", "VALUE2zzz")]);

const value1zzz = await c.env.KV.get("2929zzz");
const value2zzz = await c.env.KV.get("2930zzz");
const valueszzz = await c.env.KV.get(["2929zzz", "2930zzz"]);

return new Response(
JSON.stringify(
{
values: Object.fromEntries(values),
value1: value1,
value2: value2,
valueszzz: Object.fromEntries(valueszzz),
value1zzz: value1zzz,
value2zzz: value2zzz,
},
null,
2,
),
);
});
Result:
{
"values": {},
"value1": "VALUE",
"value2": "VALUE2",
"valueszzz": {
"2929zzz": "VALUEzzz",
"2930zzz": "VALUE2zzz"
},
"value1zzz": "VALUEzzz",
"value2zzz": "VALUE2zzz"
}
{
"values": {},
"value1": "VALUE",
"value2": "VALUE2",
"valueszzz": {
"2929zzz": "VALUEzzz",
"2930zzz": "VALUE2zzz"
},
"value1zzz": "VALUEzzz",
"value2zzz": "VALUE2zzz"
}
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?

Did you find this page helpful?