为了账号安全,请及时绑定邮箱和手机立即绑定

json: 不支持的类型: func() time.时间封送处理创建时间在当前、更新时和已删除时间

json: 不支持的类型: func() time.时间封送处理创建时间在当前、更新时和已删除时间

Go
莫回无 2022-08-09 20:37:01
如何解决尝试将新保存的模型作为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)

它对我有用,我希望它也对你有用。


查看完整回答
反对 回复 2022-08-09
?
慕后森

TA贡献1802条经验 获得超5个赞

正如其他答案所说。Create() 返回 *gorm.DB,因此您必须知道导致此错误的原因。您应该以这种方式返回错误:

if err:= database.DBConn.Create(&newUser).Error; if err != nil {
     return c.Status(fiber.StatusBadRequest).JSON(err.Error()) 
}



查看完整回答
反对 回复 2022-08-09
  • 2 回答
  • 0 关注
  • 767 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号