_Pear
_Pear•14mo ago

KV list is returning an empty iterator, but data is there and gettable via get

Hello. To simplify things, I am setting a key of ["accounts", account.name] but when I try to get the accounts with kv.list({ prefix: ["accounts"] }), I always get an empty iterator back. I am able to see the record exist when using kv.get(). Here are the relevant snippets; what am I doing wrong, or is this a bug?:
const Db = await Deno.openKv();
//...
await Db.set(["accounts", account.username], account);

//...
const iter = Db.list({ prefix: ["accounts"] }); // why is this always empty?
for await (const row of iter) {
console.log(row);
}
const Db = await Deno.openKv();
//...
await Db.set(["accounts", account.username], account);

//...
const iter = Db.list({ prefix: ["accounts"] }); // why is this always empty?
for await (const row of iter) {
console.log(row);
}
2 Replies
lcasdev
lcasdev•14mo ago
That doesn't seem right! Can you simplify this into a minimal example that I can try out locally?
_Pear
_Pear•14mo ago
sure !one moment Hmmm it seems to work fine in this isolated example:
export const Db = await Deno.openKv("./kv/kv-repro.db");

const makeAccounts = async () => {
const accounts = [
{
username: "test1",
},
{
username: "test2",
},
{
username: "test3",
},
{
username: "test4",
},
];

for (const account of accounts) {
await Db.set(["accounts", account.username], account);
}
};


const listAccounts = async () => {
const iter = Db.list({ prefix: ["accounts"] });
for await (const row of iter) {
console.log(row);
}
};

await makeAccounts();
await listAccounts();
export const Db = await Deno.openKv("./kv/kv-repro.db");

const makeAccounts = async () => {
const accounts = [
{
username: "test1",
},
{
username: "test2",
},
{
username: "test3",
},
{
username: "test4",
},
];

for (const account of accounts) {
await Db.set(["accounts", account.username], account);
}
};


const listAccounts = async () => {
const iter = Db.list({ prefix: ["accounts"] });
for await (const row of iter) {
console.log(row);
}
};

await makeAccounts();
await listAccounts();
Where it's not working for me behind a Hono route so there may be something there I'm missing, I'll report back if I figure it out. Very confused but it seems to be working now w/Hono. Not entirely sure why. Thanks 🙂