How to assign BSON _Id to a cookie (Go, Mongodb)

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

How to assign BSON _Id to a cookie (Go, Mongodb)

问题

我正在尝试创建一个Go cookie。我想将来自Mongodb的Id分配给存储在Cookie中。但是在编译时,我遇到了以下错误:

"struct literal中的未知http.Cookie字段'Id'"

以下是我的代码:

getUser := user.CheckDB()
expiration := time.Now().Add(365 * 24 * time.Hour)

// 错误出现在下一行
cookie := 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
}

提前感谢。

英文:

I was trying to create a go cookie. I want to assign Id from Mongodb to be stored in the Cookie. But while compiling I am getting an error as follows:-

"unknown http.Cookie field 'Id' in struct literal"

The following is my code:-

getUser := user.CheckDB()
expiration := time.Now().Add(365 * 24 * time.Hour)

//The Error is Caused by the Next Line
cookie := 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
}

Thanks in advance.

答案1

得分: 0

这是一个解决该问题的方案。

以下是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
}

创建Cookie:

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)
}

调用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"] // **在这里传递BSON ID**
    }
}

更多详细信息请点击这里。这个链接对我帮助很大,希望有人会发现这个答案有用。

英文:

Here is a solution for this problem.

Cookie struct below:

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
}

Cookie Creation

 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)
    }

Cookie Call

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**
        }
    }

For more details Click Here. This link helped me a lot. Hoping someone will find this answer useful.

huangapple
  • 本文由 发表于 2016年1月29日 18:50:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/35082618.html
匿名

发表评论

匿名网友

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

确定