无法在现有用户上设置电子邮件(使用Golang)。

huangapple go评论78阅读模式
英文:

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(&amp;user)
if err != nil {
	return user, err
}
var dbuser database.User
connection.Where(&quot;email = ?&quot;, user.Email).First(&amp;dbuser)
fmt.Println(&quot;User: &quot;, dbuser)
fmt.Println(&quot;User: &quot;, 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(&quot;email = ?&quot;, user.Email).First(&amp;dbuser).Error

if errors.Is(err, gorm.ErrRecordNotFound) {
		return dbuser, err
	}
fmt.Println(&quot;User: &quot;, dbuser)
fmt.Println(&quot;User: &quot;, 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(&amp;user)
if err != nil {
	return user, false, err
}
errDB := connection.Where(&quot;email = ?&quot;, user.Email).First(&amp;dbuser).Error
if errors.Is(errDB, gorm.ErrRecordNotFound) {
	return dbuser, true, errDB
}
if dbuser.Email != &quot;&quot; {
	return dbuser, false, nil
}
return user, true, nil
}

huangapple
  • 本文由 发表于 2022年5月14日 07:33:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/72236264.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定