mostprodev
mostprodev14mo ago

Deno KV Type Error

When I run await (await kv.get<typeThing>(["example"])).id (the value is an object), it returns a type error because it is a maybe type thing.
7 Replies
mostprodev
mostprodev14mo ago
The error:
Property 'id' does not exist on type 'KvEntryMaybe<typeThing>'.
Property 'id' does not exist on type '{ key: KvKey; value: null; versionstamp: null; }'.deno-ts(2339)
Property 'id' does not exist on type 'KvEntryMaybe<typeThing>'.
Property 'id' does not exist on type '{ key: KvKey; value: null; versionstamp: null; }'.deno-ts(2339)
Perhaps the problem changed. The top is the problem. Although I check that await kv.get<typeThing>(["example"]) != null, with an if statement, it still does not get rid of the error.
NDH
NDH14mo ago
let result = await kv.get<typeThing>(["example"])
// get returns a KvEntryMaybe object = {key, value, versionstamp}
// If (result.value === null) => `key was not found`
if {result.value) console.log(result.value.id);
// no error will be thrown! your error was that 'id' does not exist // on the `KvEntryMaybe` returned by the `get` method.
let result = await kv.get<typeThing>(["example"])
// get returns a KvEntryMaybe object = {key, value, versionstamp}
// If (result.value === null) => `key was not found`
if {result.value) console.log(result.value.id);
// no error will be thrown! your error was that 'id' does not exist // on the `KvEntryMaybe` returned by the `get` method.
mostprodev
mostprodev14mo ago
Cool. This is not different from != null.
NDH
NDH14mo ago
You could do (result !== null) Be aware that the object returned by the promise KvEntryMaybe = {key, value, versionstamp}, will always return the key, but if not found, both the value, and the timestamp will be null.
mostprodev
mostprodev14mo ago
Oh, okay. Thanks.
NDH
NDH14mo ago
Also, as someone could insert a row with the value set to null, it might be best to check the versionstamp for null, rather than the value. Be aware that some folks might just use the multipart key as both key and data, and just put null in the value field.
mostprodev
mostprodev14mo ago
This doesn't work.
'user_data.value' is possibly 'null'.deno-ts(18047)
(property) value: User | null
'user_data.value' is possibly 'null'.deno-ts(18047)
(property) value: User | null
Even when doing the if statement + the result declaration. Nvm.