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

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;
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.");
}
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?
9 Replies
ShaharMS
ShaharMSOP•2y ago
also, GROUP_CAL should be of type Map<String, Map<Number, Boolean>>
James
James•2y ago
You need to await the KV put. This one: env.GROUP_CAL.put(requestData.name, m); It's a promise
ShaharMS
ShaharMSOP•2y ago
when i do that, nothing updates - could that be because of the cpu runtime restriction? i dont really know jsut asking ? also no entries are registered using this, so it just doesnt work (in my case) Anybody? i dont get it is this like an answer once thing? 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 actually makes sense, will try
James
James•2y ago
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. See https://developers.cloudflare.com/workers/runtime-apis/kv/#writing-key-value-pairs for more details
ShaharMS
ShaharMSOP•2y ago
ok thanks will try writing works, but now i cant reas anything from there 🤔 it looks like nothing can get read, even if i manually input it both list() return an array with 0 length, and get() always returns null, even when values exist
James
James•2y ago
Can you provide a minimal script to reproduce what you’re seeing?
ShaharMS
ShaharMSOP•2y ago
resolved, cloudflare KV doesnt like hebrew keys actually, is that an issue? never saw that documented
James
James•2y ago
That should work fine as far as I know 🤔
ShaharMS
ShaharMSOP•2y ago
there is some overall strangeness around RTL letters, since some platforms add dpecial chars (directional chars) to know how to render them, so the case here might be windows\vscode\cludflare adding\removing these chars (and that isnt noticble since they are invisible 0-width chars...)
Want results from more Discord servers?
Add your server