英文:
can't have email on existing user golang
问题
我正在尝试检查用户是否已经拥有电子邮件。我写了一个类似这样的函数:
func IsUniqueEmail(body io.ReadCloser) (database.User, error) {
connection := database.GetDatabase()
defer database.CloseDatabase(connection)
var user database.User
err := json.NewDecoder(body).Decode(&user)
if err != nil {
return user, err
}
var dbuser database.User
connection.Where("email = ?", user.Email).First(&dbuser)
fmt.Println("User: ", dbuser)
fmt.Println("User: ", dbuser.Email)
return dbuser, nil
}
但是 fmt.Println 显示给我:
User: {{0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>} }
我假设 dbuser 中没有电子邮件,但我无法理解为什么。有人有想法吗?谢谢。
英文:
I'm trying to check if an user already have a email. I wrote a function like this :
func IsUniqueEmail(body io.ReadCloser) (database.User, error) {
connection := database.GetDatabase()
defer database.CloseDatabase(connection)
var user database.User
err := json.NewDecoder(body).Decode(&user)
if err != nil {
return user, err
}
var dbuser database.User
connection.Where("email = ?", user.Email).First(&dbuser)
fmt.Println("User: ", dbuser)
fmt.Println("User: ", dbuser.Email)
return dbuser, nil}
but the fmt.Println show me
> User: {{0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>} }
I assume where is no email in dbuser but I can't understand why
anyone have an idea ?
Cheers
答案1
得分: 1
如果你正在使用 gorm v2,你可以捕获记录未找到的错误。
var dbuser database.User
err := connection.Where("email = ?", user.Email).First(&dbuser).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return dbuser, err
}
fmt.Println("User: ", dbuser)
fmt.Println("User: ", dbuser.Email)
return dbuser, nil
请注意,这是一个示例代码片段,用于在使用 gorm v2 时捕获记录未找到的错误。你可以根据自己的需求进行修改和适配。
英文:
If you are using gorm v2, you can catch the record not found error
var dbuser database.User
err := connection.Where("email = ?", user.Email).First(&dbuser).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return dbuser, err
}
fmt.Println("User: ", dbuser)
fmt.Println("User: ", dbuser.Email)
return dbuser, nil}
答案2
得分: 0
解决方案
func IsUniqueEmail(body io.ReadCloser) (database.User, bool, error) {
var dbuser database.User
connection := database.GetDatabase()
defer database.CloseDatabase(connection)
var user database.User
err := json.NewDecoder(body).Decode(&user)
if err != nil {
return user, false, err
}
errDB := connection.Where("email = ?", user.Email).First(&dbuser).Error
if errors.Is(errDB, gorm.ErrRecordNotFound) {
return dbuser, true, errDB
}
if dbuser.Email != "" {
return dbuser, false, nil
}
return user, true, nil
}
英文:
The solution
func IsUniqueEmail(body io.ReadCloser) (database.User, bool, error) {
var dbuser database.User
connection := database.GetDatabase()
defer database.CloseDatabase(connection)
var user database.User
err := json.NewDecoder(body).Decode(&user)
if err != nil {
return user, false, err
}
errDB := connection.Where("email = ?", user.Email).First(&dbuser).Error
if errors.Is(errDB, gorm.ErrRecordNotFound) {
return dbuser, true, errDB
}
if dbuser.Email != "" {
return dbuser, false, nil
}
return user, true, nil
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论