vercel / storage

Vercel Postgres, KV, Blob, and Edge Config
https://vercel.com/storage
Apache License 2.0
517 stars 58 forks source link

kv.hgetall not working with middleware #674

Closed devklepacki closed 6 months ago

devklepacki commented 6 months ago

Hello, I noticed that kv.hgetall seems to be always returning null on middleware. Here is how I do it:

import { kv } from '@vercel/kv'; // @vercel/kv@1.0.1

// setting data works correctly
// await kv.hset(username, { [pin]: body.url })

await kv.hset('foobar', { '1834': 'https://example.com' })
await kv.hset('foobar', { '1314': 'https://example2.com' })
// retriving specific data works correctly
// await kv.hget(username, pin)

let data = await kv.hget('foobar', '1834')
console.log(data) // 'https://example.com'

data = await kv.hget('foobar', '1314')
console.log(data) // 'https://example2.com'
// retriving all data returns null
// await kv.hgetall(username)

const data = await kv.hgetall('foobar')
console.log(data) // null

While it does work from Vercel CLI :

hgetall foobar
// 1834, https://example.com, 1314, https://example2.com

Am I doing something wrong?

luismeyer commented 6 months ago

hey @devklepacki, I can't reproduce this with the code you shared. Could you create a minimal reproduction repository for this issue?

devklepacki commented 6 months ago

Omg. Just wanted to send a reproduction when I realized something. kv.hgetall is returning an actual kv object, not a json string to be parsed. Eh so much human time wasted on that! It was my typeguard failing, not the hgetall. Are there type definition available for the @vercel/kv package? I'm unable to find any.

luismeyer commented 6 months ago

ah, no worries. Yes @vercel/kv comes with type definitions, specifically we just expose the upstash/redis-client typings. If you are looking for code it is here: https://github.com/upstash/upstash-redis/blob/main/pkg/commands/hgetall.ts