reddit / devvit

Reddit for Developers
https://developers.reddit.com
BSD 3-Clause "New" or "Revised" License
59 stars 13 forks source link

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

Open shiruken opened 1 month ago

shiruken commented 1 month ago

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]