无法使FindId工作(GO + MGO)。

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

Can't get FindId to work (GO + MGO)

问题

不确定这里发生了什么...但是我试图完成一个简单的操作时遇到了很大的困难。我刚开始学习GO(试图从Node切换过来),所以可能是类型的问题...

  1. User struct {
  2. ID_ bson.ObjectId `bson:"_id,omitempty" json:"_id,omitempty"`
  3. UTC time.Time `bson:"utc,omitempty" json:"utc,omitempty"`
  4. USR string `bson:"usr,omitempty" json:"usr,omitempty"`
  5. PWD string `bson:"pwd,omitempty" json:"pwd,omitempty"`
  6. }
  7. func save(w http.ResponseWriter, r *http.Request) {
  8. m := s.Copy()
  9. defer m.Close()
  10. user := m.DB("0").C("user")
  11. var a User
  12. json.NewDecoder(r.Body).Decode(&a)
  13. err := user.FindId(a.ID_)
  14. if err != nil {
  15. panic(err)
  16. }
  17. }

这会返回以下错误

  1. http: panic serving [::1]:53092: &{{0 0} 0xc208062600 {{0.user [{_id TE?????}] 0 0 ?
  2. reflect.Value? 0 <nil> {?reflect.Value? ?reflect.Value? ?reflect.Value? false false [] 0}
  3. false []} 0.25 0}}

当我运行:

  1. a.ID_.Valid()

我得到了"true"。

附注:我可以让这个工作起来:

  1. func user(w http.ResponseWriter, r *http.Request) {
  2. m := s.Copy()
  3. defer m.Close()
  4. user := m.DB("0").C("user")
  5. a := &User{ID_:bson.NewObjectId(), UTC:time.Now()}
  6. b, _ := json.Marshal(a)
  7. user.Insert(a)
  8. }

非常感谢任何帮助。

英文:

Not sure what's going on here... but I'm having a heck of a time trying to get a simple operation done. I'm new to GO (trying to switch from Node) so it's probably a Type thing...

  1. User struct {
  2. ID_ bson.ObjectId `bson:&quot;_id,omitempty&quot; json:&quot;_id,omitempty&quot;`
  3. UTC time.Time `bson:&quot;utc,omitempty&quot; json:&quot;utc,omitempty&quot;`
  4. USR string `bson:&quot;usr,omitempty&quot; json:&quot;usr,omitempty&quot;`
  5. PWD string `bson:&quot;pwd,omitempty&quot; json:&quot;pwd,omitempty&quot;`
  6. }
  7. func save(w http.ResponseWriter, r *http.Request) {
  8. m := s.Copy()
  9. defer m.Close()
  10. user := m.DB(&quot;0&quot;).C(&quot;user&quot;)
  11. var a User
  12. json.NewDecoder(r.Body).Decode(&amp;a)
  13. err := user.FindId(a.ID_)
  14. if err != nil {
  15. panic(err)
  16. }
  17. }

This returns the following error

  1. http: panic serving [::1]:53092: &amp;{{0 0} 0xc208062600 {{0.user [{_id TE?????}] 0 0 ?
  2. reflect.Value? 0 &lt;nil&gt; {?reflect.Value? ?reflect.Value? ?reflect.Value? false false [] 0}
  3. false []} 0.25 0}}

When I run:

  1. a.ID_.Valid()

I get "true".

PS. I can get this to work:

  1. func user(w http.ResponseWriter, r *http.Request) {
  2. m := s.Copy()
  3. defer m.Close()
  4. user := m.DB(&quot;0&quot;).C(&quot;user&quot;)
  5. a := &amp;User{ID_:bson.NewObjectId(), UTC:time.Now()}
  6. b, _ := json.Marshal(a)
  7. user.Insert(a)
  8. }

Any help would be really appreciated.

答案1

得分: 4

FindId 方法返回一个 Query 对象,而不是一个错误。调用 Query 的 One 方法来获取文档。

英文:

The FindId method returns a Query, not an error. Call the Query One method to get the document.

答案2

得分: 3

根据文档 http://godoc.org/labix.org/v2/mgo#Collection.FindId

FindId 返回一个 Query 结构体,你可以在其上调用任何函数。FindId 不会返回错误。

尝试使用以下代码:

  1. var userDoc interface{}
  2. if err := user.FindId(a.ID_).One(&userDoc); err != nil {
  3. panic(err)
  4. }

你可以将 interface{} 替换为你用于用户的任何结构体。

英文:

As per the docs http://godoc.org/labix.org/v2/mgo#Collection.FindId

FindId returns a Query struct which you can then call any of its functions. FindId does not return an error.

Try

  1. var userDoc interface{}
  2. if err := user.FindId(a.ID_).One(&amp;userDoc); err != nil {
  3. panic(err)
  4. }

You can change interface{} with whatever struct you are using for users.

huangapple
  • 本文由 发表于 2014年10月21日 08:19:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/26477029.html
匿名

发表评论

匿名网友

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

确定