go-redis icon indicating copy to clipboard operation
go-redis copied to clipboard

Issue with HgetAll

Open dagenius007 opened this issue 1 year ago • 1 comments

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"

dagenius007 avatar Nov 28 '23 18:11 dagenius007

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:    "[email protected]",
		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:[email protected] Code:s1002 Password:hello}
dump mapper => map[code:s1002 email:[email protected] password:hello]

rfyiamcool avatar Dec 21 '23 05:12 rfyiamcool