Weird KV behavior (metadata and value set as values)
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
- 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"
})
- 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
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'}} )
Thanks for reporting, we've passed this to the KV team for more investigation.
This issue hasn't been updated in a while. If it's still reproducing, please comment to let us know. Thank you!
Don't know why this closed. We are actively working on it with internal ticket KV-1524.
@markjmiller I reactivated this one. Looks like you got it done from the Jira? Not sure if this one has been released yet.
Fixed in https://github.com/cloudflare/cloudflare-typescript/commit/d4ce21e135415971b6b4578151304289f5b7c4ef
This issue hasn't been updated in a while. If it's still reproducing, please comment to let us know. Thank you!
This issue hasn't been updated in a while. If it's still reproducing, please comment to let us know. Thank you!