Usage
Returns the value associated with field in the hash stored at key.
await redis.hget(key, field);
Parameters
The field in the hash to get
Response
The value associated with the field, or null when the field is not present in the hash or the key does not exist.
Examples
Get a hash field value
import { Redis } from '@upstash/redis';
const redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL!,
token: process.env.UPSTASH_REDIS_REST_TOKEN!,
});
// Set a hash field
await redis.hset('user:1000', { name: 'Alice', email: 'alice@example.com' });
// Get a specific field
const name = await redis.hget('user:1000', 'name');
console.log(name); // 'Alice'
// Get a non-existent field
const age = await redis.hget('user:1000', 'age');
console.log(age); // null
Working with typed data
interface User {
name: string;
email: string;
}
// Get with type inference
const email = await redis.hget<string>('user:1000', 'email');
console.log(email); // 'alice@example.com'
See Also
- HSET - Set hash field values
- HGETALL - Get all fields and values in a hash
- HMGET - Get multiple hash field values