如何解决尝试将新保存的模型作为JSON返回的错误。json: unsupported type: func() time.Timeuser := database.DBConn.Create(&newUser)return c.Status(http.StatusCreated).JSON(user) // fiber response已尝试忽略时间戳字段。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:"-"`}还尝试了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"`和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"`使用 gorm v1.20.12 和 Go 1.15.6
2 回答

神不在的星期二
TA贡献1963条经验 获得超6个赞
我遇到了类似的错误,发现返回一个*gorm。无法由 JSON 封送的 DB。user := database.DBConn.Create(&newUser)
请改用这个:
database.DBConn.Create(&newUser)
return c.Status(http.StatusCreated).JSON(newUser)
它对我有用,我希望它也对你有用。

慕后森
TA贡献1802条经验 获得超5个赞
正如其他答案所说。Create() 返回 *gorm.DB,因此您必须知道导致此错误的原因。您应该以这种方式返回错误:
if err:= database.DBConn.Create(&newUser).Error; if err != nil { return c.Status(fiber.StatusBadRequest).JSON(err.Error()) }
- 2 回答
- 0 关注
- 767 浏览
添加回答
举报
0/150
提交
取消