How can I resolve the error json: unsupported type: func() time.Time
trying to return a newly saved model back as JSON.
user := database.DBConn.Create(&newUser)
return c.Status(http.StatusCreated).JSON(user) // fiber response
Tried ignoring the timestamp fields.
type User struct {
gorm.Model
ID uuid.UUID `gorm:"primary_key;type:uuid;default:uuid_generate_v4()"`
Phone string `json:"phone"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Email string `json:"email"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt time.Time `json:"-"`
}
Also tried
CreatedAt time.Time `gorm:"type:timestamp" json:"created_at,string,omitempty"`
UpdatedAt time.Time `gorm:"type:timestamp" json:"updated_at,string,omitempty"`
DeletedAt time.Time `gorm:"type:timestamp" json:"deleted_at,string,omitempty"`
And
CreatedAt time.Time `gorm:"type:datetime" json:"created_at,string,omitempty"`
UpdatedAt time.Time `gorm:"type:datetime" json:"updated_at,string,omitempty"`
DeletedAt time.Time `gorm:"type:datetime" json:"deleted_at,string,omitempty"`
Using gorm v1.20.12 and Go 1.15.6
I encountered similar error and discovered that user := database.DBConn.Create(&newUser)
returns a *gorm.DB which cannot be marshaled by JSON.
Instead use this:
database.DBConn.Create(&newUser)
return c.Status(http.StatusCreated).JSON(newUser)
It worked for me and I hope it works for you as well.