Skip to content

Weird KV behavior (metadata and value set as values) #2593

@Spioune

Description

@Spioune

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

  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions