cloudflare-typescript icon indicating copy to clipboard operation
cloudflare-typescript copied to clipboard

Weird KV behavior (metadata and value set as values)

Open Spioune opened this issue 9 months ago • 2 comments

Confirm this is a Typescript library issue and not an underlying Cloudflare API issue

  • [x] This is an issue with the Typescript library

Describe the bug

When I set a KV value with

await cf.kv.namespaces.values.update(env.CF_NAMESPACE_ID, "key1", {
    account_id: env.CF_ACCOUNT_ID,
    metadata: "", // why is metadata mandatory ?
    value: JSON.stringify({
      hello:"world"
    })
  })

It actually sets the KV to {metadata:"", value:{hello:"world"}} So now in a worker if I try to get the value with

const value = await c.env.KV.get<{hello: string}>("key1", "json");

I expect the get back the object {hello:'world'} but instead I get {metadata:'', value:"{'hello':'world'}"} So to fix this weird behavior I need to do

const value = await c.env.KV.get<{value:string}>("key1", 'json');
const parsedValue = JSON.parse(value.value)

I can see the metadata on the Cloudflare KV web UI (in the value column), which is not the case for KV set from Cloudflare Workers with KV binding

To Reproduce

  1. Set a KV value with the Cloudflare typescript library
await cf.kv.namespaces.values.update(env.CF_NAMESPACE_ID, "key1", {
    account_id: env.CF_ACCOUNT_ID,
    metadata: "", // why is metadata mandatory ?
    value: "value1"
  })
  1. Get the KV in worker with KV binding
const value = await c.env.KV.get("key1");

Instead of getting back "value1", I get back "{'metadata':'','value':'value1'}"

Code snippets


OS

macOS

Runtime version

^5

Library version

v4.2.0

Spioune avatar Apr 06 '25 12:04 Spioune

note that the bulkupdate() method doesn't have this issue.

await cf.kv.namespaces.bulkUpdate(env.CF_NAMESPACE_ID, {
    account_id: env.CF_ACCOUNT_ID,
    body: myarray.map(item => ({
        key: item.key, // here metadata is not mandatory contrary to values.update() method...
        value: JSON.stringify({ hello: 'world' }),
    })),
});

This inserts {hello:'world'} correctly. (values.update() would have inserted {value:{hello:'world'}} )

Spioune avatar Apr 07 '25 14:04 Spioune

Thanks for reporting, we've passed this to the KV team for more investigation.

jhutchings1 avatar Apr 07 '25 21:04 jhutchings1

This issue hasn't been updated in a while. If it's still reproducing, please comment to let us know. Thank you!

github-actions[bot] avatar Jun 07 '25 16:06 github-actions[bot]

Don't know why this closed. We are actively working on it with internal ticket KV-1524.

markjmiller avatar Jun 17 '25 22:06 markjmiller

@markjmiller I reactivated this one. Looks like you got it done from the Jira? Not sure if this one has been released yet.

jhutchings1 avatar Jun 19 '25 00:06 jhutchings1

Fixed in https://github.com/cloudflare/cloudflare-typescript/commit/d4ce21e135415971b6b4578151304289f5b7c4ef

1000hz avatar Jul 10 '25 16:07 1000hz

This issue hasn't been updated in a while. If it's still reproducing, please comment to let us know. Thank you!

github-actions[bot] avatar Sep 08 '25 16:09 github-actions[bot]

This issue hasn't been updated in a while. If it's still reproducing, please comment to let us know. Thank you!

github-actions[bot] avatar Nov 08 '25 16:11 github-actions[bot]