#env.KV_NAMESPACE.put(...) never actually writes anything

26 messages · Page 1 of 1 (latest)

split sand
#

i might be dong this icorrectly, if so, please tell me:
i connected the KV to my worker in the toml file. that also shows up on cloudflare's website. whenever the user sends json data, it should get written:

const requestData = await request.json()
currentUsername = requestData.name;
if (acceptedNames.includes(requestData.name)) {
    acceptedEntry = true;
    var m = new Map();
    for (let i = 0; i < requestData.array.length; i += 2) {
        m[requestData.array[i]] = requestData.array[i + 1];
    }
    env.GROUP_CAL.put(requestData.name, m);
    groupCal.set(requestData.name, m);
} else acceptedEntry = false;

even though requestData is defined, and groupCal gets written to, it seems like GROUP_CAL isnt getting written to, since when returning the new site (or even retrieving it a couple minutes after the put), and writing from
GROUP_CAL to groupCal, the result is always {}:

for (const name of acceptedNames) {
    let val = await env.GROUP_CAL.get(name);
    if (val !== null) {
        groupCal[name] = val;
    } else console.log(name + " has no data.");
}

what am i doing wrong?

#

also, GROUP_CAL should be of type Map<String, Map<Number, Boolean>>

wraith phoenix
#

You need to await the KV put. This one: env.GROUP_CAL.put(requestData.name, m);

#

It's a promise

split sand
#

when i do that, nothing updates - could that be because of the cpu runtime restriction?

#

i dont really know jsut asking

#

?

split sand
split sand
#

Anybody?

split sand
#

i dont get it is this like an answer once thing?

round wren
#

should you not stringify the KV value?

#

are you not getting anything in the logs?

split sand
#

nothing in the logs, is the value supposed to be stringified? never saw it in the docs

#

nothing ever in the logs actually, even when the worker errors

split sand
wraith phoenix
#

value in a KV.put call needs to be a string, ReadableStream, or ArrayBuffer. Any other type is likely to be serialised to {} or something alike.

#

You will need to both await the put calls, and convert to a data type that you can write.

split sand
#

ok thanks will try

split sand
#

writing works, but now i cant reas anything from there 🤔

#

it looks like nothing can get read, even if i manually input it

split sand
#

both list() return an array with 0 length, and get() always returns null, even when values exist

wraith phoenix
#

Can you provide a minimal script to reproduce what you’re seeing?

split sand
#

resolved, cloudflare KV doesnt like hebrew keys

#

actually, is that an issue? never saw that documented

wraith phoenix
#

That should work fine as far as I know 🤔