我想在使用 gorm 進行查詢后獲取用戶數據item := []models.User{}if config.DB.First(&item, "email = ?", email).RecordNotFound() || len(item) == 0 { c.JSON(500, gin.H{ "status": "error", "message": "record not found"}) c.Abort() return}c.JSON(200, gin.H{ "status": "success", "data": item,})這是我的模型type User struct {gorm.ModelStores []Store // to show that customer can have many storesUserName stringFullName stringEmail string `gorm:"unique_index"`Password stringSocialID stringProvider stringAvatar stringRole bool `gorm:"default:0"`HaveStore bool `gorm:"default:0"`isActivate bool `gorm:"default:0"`}我只想在從gorm查詢后獲取用戶名,如何獲?。课艺谑褂胕tem.Username但是,錯誤表明item.UserName undefined (type []models.User has no field or method UserName)
2 回答

大話西游666
TA貢獻1817條經驗 獲得超14個贊
您正試圖UserName從部分用戶那里得到問題。如果電子郵件是唯一的字段數據庫,那么您可以只使用用戶模型而不是使用用戶切片。
item := models.User{}
config.DB.First(&item, "email = ?", email)
然后你可以像用戶名一樣訪問item.UserName
- 2 回答
- 0 關注
- 550 瀏覽
添加回答
舉報
0/150
提交
取消