亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何將 BSON _Id 分配給 cookie (Go, Mongodb)

如何將 BSON _Id 分配給 cookie (Go, Mongodb)

Go
哈士奇WWW 2021-12-07 16:43:46
我試圖創建一個 go cookie。我想從 Mongodb 分配 Id 以存儲在 Cookie 中。但是在編譯時我收到如下錯誤:-“結構文字中未知的 http.Cookie 字段‘Id’”以下是我的代碼:-getUser := user.CheckDB()expiration := time.Now().Add(365 * 24 * time.Hour)//The Error is Caused by the Next Linecookie := http.Cookie{Id: getUser[0].Id, Name: getUser[0].Email, Value: getUser[0].Password, Expires: expiration}http.SetCookie(w, &cookie)func (this *User) CheckDB() []User {    var results []User    sess, db := GetDatabase()    defer sess.Close()    c := db.C("user")    uname := &this.Email    err := c.Find(bson.M{"email": *uname}).Sort("-id").All(&results)    if err != nil {        panic(err)    } else {        fmt.Println("Results All: ", results)        return results    }}type Cookie struct {    Id         bson.ObjectId `bson:"_id,omitempty"`    Name       string    Value      string    Path       string    Domain     string    Expires    time.Time    RawExpires string    MaxAge     int    Secure     bool    HttpOnly   bool    Raw        string    Unparsed   []string}提前致謝。
查看完整描述

1 回答

?
森欄

TA貢獻1810條經驗 獲得超5個贊

這是此問題的解決方案。


Cookie 結構如下:


type Cookie struct {    

    Name       string

    Value      string

    Path       string

    Domain     string

    Expires    time.Time

    RawExpires string

    MaxAge   int

    Secure   bool

    HttpOnly bool

    Raw      string

    Unparsed []string

}

餅干制作


 value := map[string]string{

        "id": cookieId,

    }

    if encoded, err := ckieHandler.Encode("session", value); err == nil {

        cookie := &http.Cookie{

        Name:  "session",

        Value: encoded,

        Path:  "/",

        }

        http.SetCookie(response, cookie)

    }

餅干呼叫


if cookie, err := request.Cookie("session"); err == nil {

        cookieValue := make(map[string]string)

        if err = ckieHandler.Decode("session", cookie.Value, &cookieValue); err == nil {

            id = cookieValue["id"] // **Pass BSON ID here**

        }

    }

有關更多詳細信息,請單擊此處。這個鏈接對我幫助很大。希望有人會發現這個答案有用。


查看完整回答
反對 回復 2021-12-07
  • 1 回答
  • 0 關注
  • 185 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號