首页 > 解决方案 > 邮递员不验证现有的电子邮件 golang

问题描述

我正在用 Go 语言做一个项目,我已经在 mongodb 中创建了我的集合,并且我创建了插入用户数据并验证电子邮件和昵称的函数(如果它们有这么多字符或者它们已经存在于数据库中)问题是当在邮递员中尝试时,会验证两者中的任何一个是否具有字符,但如果它们存在于数据库中则不会验证,只需将它们始终以相同的用户和相同的电子邮件插入数据库,我觉得代码还不错,你怎么看?

//这是验证用户是否存在的功能,我为电子邮件制作了另一个完全相同的功能

func CheckUserExist(nick string) (models.Useer, bool, string) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()

db := MongoC.Database("DebMan_N01")
col := db.Collection("users")

condition := bson.M{"nick": nick}

var results models.Useer

err := col.FindOne(ctx, condition).Decode(&results)

ID := results.ID.Hex()
if err != nil {
    return results, false, ID
}
return results, true, ID }

//这里我调用了函数,并表示如果它返回的值为真,即如果为真则用户已经存在

    _, founded, _ := bd.CheckUserExist(t.Nick)
if founded == true {
    http.Error(w, "Ya existe un nombre de usuario registrado", 400)
    return
}
_, foundedE, _ := bd.CheckEmailExist(t.Email)
if foundedE == true {
    http.Error(w, "Ya existe un usuario con el email", 400)
    return
}

在此处输入图像描述

标签: mongodbvalidationgopostmanregistration

解决方案


推荐阅读