第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang初學(xué)者無法解決問題

Golang初學(xué)者無法解決問題

Go
藍(lán)山帝景 2022-10-10 19:56:26
我想通過這種方法進(jìn)行用戶登錄。這必須分為三個部分。與用戶注冊相同,但我不明白如何做到這一點?您能否寫下我如何以同樣的方式編寫用戶登錄邏輯?它是使用 gorilla mux 創(chuàng)建的。一種方法必須在 db_service.go 中,然后一種方法必須在 login_service.go 中,一種方法必須在 login.go 中。這是 db_service.go 代碼:  /* Used to create a singleton object of MongoDB client.Initialized and exposed through  GetMongoClient().*/var clientInstance *mongo.Client//Used during creation of singleton client object in GetMongoClient().var clientInstanceError error//Used to execute client creation procedure only once.var mongoOnce sync.Once//I have used below constants just to hold required database config's.const (    CONNECTIONSTRING = "http://127.0.0.1:27017"    AUTH_DB          = "Cluster0"    USER_COLLECTION  = "user")//GetMongoClient - Return mongodb connection to work withfunc GetMongoClient() (*mongo.Client, error) {    //Perform connection creation operation only once.    mongoOnce.Do(func() {        // Set client options        clientOptions := options.Client().ApplyURI(CONNECTIONSTRING)        // Connect to MongoDB        client, err := mongo.Connect(context.TODO(), clientOptions)        if err != nil {            clientInstanceError = err        }        // Check the connection        err = client.Ping(context.TODO(), nil)        if err != nil {            clientInstanceError = err        }        log.Println("Connected Mongodb!")        clientInstance = client    })    return clientInstance, clientInstanceError}//CreateIssue - Insert a new document in the collection.func User_Collection(user *model.User) (*mongo.InsertOneResult, error) {    //Create a handle to the respective collection in the database.    collection := clientInstance.Database(AUTH_DB).Collection(USER_COLLECTION)    //Perform InsertOne operation & validate against the error.    return collection.InsertOne(context.TODO(), user)}
查看完整描述

1 回答

?
慕哥6287543

TA貢獻(xiàn)1831條經(jīng)驗 獲得超10個贊

像這樣的處理程序


func Login(w http.ResponseWriter, r *http.Request) {

    w.Header().Set("Content-Type", "application/json")


    var user model.User

    if err := json.NewDecoder(r.Body).Decode(&user); err == nil {


      if _, err := service.Login_User(&user); err == nil {

          json.NewEncoder(w).Encode(user)

      } else {

          w.WriteHeader(http.StatusInternalServerError)

          json.NewEncoder(w).Encode(err)

      }


    } else {

          http.Error(w, err.Error(), http.StatusBadRequest)

          return

    }

}

像這樣的數(shù)據(jù)庫(需要實現(xiàn)查詢而不是插入)


 func Login_User(user *model.User) (interface{}, error) {


    // Implement this query

    if result, err := util.Find_User(user); err == nil {

        return result, err

    } else {

        return nil, err

    }

}


查看完整回答
反對 回復(fù) 2022-10-10
  • 1 回答
  • 0 關(guān)注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號