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

Open
#79 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
redis, typescript
Domain
databases

Research direction

Start with packages/public-api/src/types/redis.ts at the hgetall declaration and trace the corresponding implementation and tests. Reproduce the missing-key case described in the issue, then verify that hgetall returns undefined consistently with the stated typehint and the existing get and hget behavior.

Written by the indexing model from the issue text.

Description

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]

Dominant language
TypeScript
Stars
210
Forks
88
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from reddit/devvit

All issues in reddit/devvit

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.