KV giving null while doing get even though the key exists

There is no error from KV and also there is no response with correct value.
15 Replies
JustinNoel
JustinNoel13mo ago
You'll probably need to give us more context on the problem. Do you have a code sample or something? Are you testing locally or are you working in production?
ASK
ASKOP13mo ago
Production @JustinNoel The code snippet is as follows:
let res = await env.KV_NAME.get(kv_id)
.then((r) => {
console.log("Successful response from KV => ", r);
return r;
})
.catch((e) => {
console.warn(" error in getting the from kv ", e);
return null;
});
let res = await env.KV_NAME.get(kv_id)
.then((r) => {
console.log("Successful response from KV => ", r);
return r;
})
.catch((e) => {
console.warn(" error in getting the from kv ", e);
return null;
});
Here is the sample code... Sometimes, I'm getting null in the then part of the response, not the catch part. So the request is successful while the return response from kv is null even though there is value for the said key
Hello, I’m Allie!
Errors are only thrown when something goes wrong internally. Since a get of an empty key isn't wrong, then no error is thrown. Also, you seem to be combining await and then
JustinNoel
JustinNoel13mo ago
Are you testing locally or are you working in production?
ASK
ASKOP13mo ago
The key itself is present and has value in kv store Mistake on my part Production
Hello, I’m Allie!
No, I just meant that, if a key wasn't present, that wouldn't be an error, so it wouldn't hit the catch handler. How long has it been since it was inserted/updated? Updates can take 60 seconds or more to show up, depending on how recently you tried to access the key, and what cacheTtl you set, if any.
ASK
ASKOP13mo ago
Right About 1 week
JustinNoel
JustinNoel13mo ago
Try just doing this:
let res = await env.KV_NAME.get(kv_id);
console.log(`res = ${res}`)
let res = await env.KV_NAME.get(kv_id);
console.log(`res = ${res}`)
If you to catch errors, I would suggest using a try catch instead of then and catch.
ASK
ASKOP13mo ago
I'm not getting error. The kv is just returning null even though value for the key exists
Michał @ yournextstore.com
How do you know it exists?
VMSS
VMSS13mo ago
By directly checking in the KV store, by searching with the key
JustinNoel
JustinNoel13mo ago
Is KV_NAME bound to the correct KV namespace?
ASK
ASKOP13mo ago
Yes
Tin Cap
Tin Cap13mo ago
Can you sanity check that by adding something in the KV web view and then trying to retrieve it in your code? I very much suspect there is an issue on your side somewhere.
ASK
ASKOP13mo ago
I did do that too... No dice
Want results from more Discord servers?
Add your server