redis / go-redis

Redis Go client
https://redis.uptrace.dev
BSD 2-Clause "Simplified" License
19.89k stars 2.34k forks source link

Issue with HgetAll #2813

Open dagenius007 opened 9 months ago

dagenius007 commented 9 months ago

I am trying to get map value from a dynamic key but it keeps returning empty value

Detailed Description


type UserTempSignup struct {
    Email    string `redis:"email" json:"email"`
    Code     string `redis:"code" json:"code"`
    Password string `redis:"password" json:"password"`
}

uid := uuid.Must(uuid.NewRandom()).String()

temp_brand := UserTempSignup{
        Email:    brand.Email,
        Code:     code,
        Password: brand.Password,
    }

err = configs.RedisClient.HSet(ctx, uid, temp_brand).Err()

    if err != nil {
        fmt.Println("err--", err)
        return c.JSON(http.StatusInternalServerError, map[string]interface{}{
            "success": false,
            "message": "An error occurred",
        })
    }
var temp_brand UserTempSignup

// validate_otp.Uid is from request body
err = configs.RedisClient.HGetAll(ctx, validate_otp.Uid).Scan(&temp_brand)

temp_brand is empty but it returns a value if I pass a static key e.g "key"

rfyiamcool commented 8 months ago

I don't find error ?

test code

package main

import (
    "context"
    "fmt"

    "github.com/redis/go-redis/v9"
)

type UserTempSignup struct {
    Email    string `redis:"email" json:"email"`
    Code     string `redis:"code" json:"code"`
    Password string `redis:"password" json:"password"`
}

func main() {
    ctx := context.Background()

    rdb := redis.NewClient(&redis.Options{
        Addr: ":6379",
    })
    _ = rdb.FlushDB(ctx).Err()

    uid := "uid:1"
    u1 := UserTempSignup{
        Email:    "haha@gmail.com",
        Code:     "s1002",
        Password: "hello",
    }

    err := rdb.HSet(ctx, uid, u1).Err()
    fmt.Println("hset", err)

    temp := new(UserTempSignup)
    cmd := rdb.HGetAll(ctx, uid)
    fmt.Println("hgetall", cmd.Err())

    cmd.Scan(temp)

    fmt.Printf("dump object => %+v\n", temp)
    fmt.Printf("dump mapper => %+v", cmd.Val())
}

output:

hset <nil>
hgetall <nil>
dump object => &{Email:haha@gmail.com Code:s1002 Password:hello}
dump mapper => map[code:s1002 email:haha@gmail.com password:hello]