devvit icon indicating copy to clipboard operation
devvit copied to clipboard

[Bug] `hgetall` returns an empty object rather than `undefined` if the key does not exist

Open shiruken opened this issue 1 year ago • 0 comments

Info

If a given key does not exist, hgetall returns an empty object rather than undefined as the typehint suggests.

https://github.com/reddit/devvit/blob/fb29ce4fb63b82bc0d8b91f9a6c0c76688d6ae02/packages/public-api/src/types/redis.ts#L511

This object unfortunately evaluates as truthy and therefore requires the user to check if keys are present in the object to determine whether the hash actually existed.

This appears to differ from the current behavior of both get and hget.

Example

const foo = await context.redis.hgetall('foo'); // no hash actually exists at key 'foo'
if (foo) {
  console.log('foo is true'); // this will be output
}

console.log(foo); // [Object object]

shiruken avatar Jul 19 '24 21:07 shiruken