在使用go-gin和mgo从MongoDB获取投票列表时出现错误。

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

Errors Getting list of polls from mongoDB with go-gin and mgo

问题

你好,以下是翻译好的内容:

嗨,我正在尝试使用Go语言的go-gin和mgo构建一个Web服务,我有一个带有MongoDB的数据库,但每次我尝试从数据库中获取投票时,我都会从Go Web服务器中获得以下错误。

我的代码如下:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "gopkg.in/gin-gonic/gin.v1"
  8. "gopkg.in/mgo.v2"
  9. )
  10. type Poll struct {
  11. ID string `json:"id,omitempty"`
  12. Firstname string `json:"firstname,omitempty"`
  13. Lastname string `json:"lastname,omitempty"`
  14. Poll string `json:"poll,omitempty"`
  15. }
  16. var (
  17. // Session stores mongo session
  18. Session *mgo.Session
  19. // Mongo stores the mongodb connection string information
  20. Mongo *mgo.DialInfo
  21. )
  22. const (
  23. // MongoDBUrl is the default mongodb url that will be used to connect to the
  24. // database.
  25. MongoDBUrl = "mongodb://localhost:27017/smartpoll"
  26. // CollectionPoll holds the name of the articles collection
  27. CollectionPoll = "polls"
  28. )
  29. // Connect connects to mongodb
  30. func Connect() {
  31. uri := os.Getenv("MONGODB_URL")
  32. if len(uri) == 0 {
  33. uri = MongoDBUrl
  34. }
  35. mongo, err := mgo.ParseURL(uri)
  36. s, err := mgo.Dial(uri)
  37. if err != nil {
  38. fmt.Printf("Can't connect to mongo, go error %v\n", err)
  39. panic(err.Error())
  40. }
  41. s.SetSafe(&mgo.Safe{})
  42. fmt.Println("Connected to", uri)
  43. Session = s
  44. Mongo = mongo
  45. }
  46. func init() {
  47. Connect()
  48. }
  49. func main() {
  50. port := os.Getenv("PORT")
  51. if port == "" {
  52. log.Fatal("$PORT must be set")
  53. }
  54. router := gin.Default()
  55. router.GET("/", func(c *gin.Context) {
  56. c.JSON(http.StatusOK, gin.H{"message": "OK"})
  57. })
  58. router.GET("/polls", allPolls)
  59. router.Run(":" + port)
  60. }
  61. func allPolls(c *gin.Context) {
  62. db := c.MustGet("db").(*mgo.Database)
  63. polls := []Poll{}
  64. poll := db.C(CollectionPoll).Find(&polls)
  65. c.JSON(http.StatusOK, gin.H{
  66. "_id": "ID",
  67. "firstname": "Firstname",
  68. "lastname": "Lastname",
  69. "poll": poll,
  70. })
  71. }

我的数据库如下:

  1. {
  2. "_id" : ObjectId("58d9cf1cdf353f3d2f5951b4"),
  3. "id" : "1",
  4. "firstname" : "Sam",
  5. "lastname" : "Smith",
  6. "poll" : "Who is the Richest Man in the World"
  7. }

希望对你有帮助!

英文:

Hi trying to build a web service with go-gin and mgo on the Go language, i have a database with mongoDB but each time i try to GET polls from the database I get errors from the folowing errors from the Go webserver.

  1. 2:00:16 PM web.1 | gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
  2. 2:00:16 PM web.1 | /home/go/src/gopkg.in/gin-gonic/gin.v1/context.go:180 (0x4e1eb1)
  3. 2:00:16 PM web.1 | (*Context).MustGet: panic("Key \"" + key + "\" does not exist")
  4. 2:00:16 PM web.1 | /home/go/src/smartpoll/main.go:139 (0x401655)
  5. 2:00:16 PM web.1 | allPolls: db := c.MustGet("db").(*mgo.Database)
  6. 2:00:16 PM web.1 | /home/go/src/gopkg.in/gin-gonic/gin.v1/context.go:97 (0x4e187a)
  7. 2:00:16 PM web.1 | (*Context).Next: c.handlers[c.index](c)
  8. 2:00:16 PM web.1 | /home/go/src/gopkg.in/gin-gonic/gin.v1/recovery.go:45 (0x4f165a)
  9. 2:00:16 PM web.1 | RecoveryWithWriter.func1: c.Next()
  10. 2:00:16 PM web.1 | /home/go/src/gopkg.in/gin-gonic/gin.v1/context.go:97 (0x4e187a)
  11. 2:00:16 PM web.1 | (*Context).Next: c.handlers[c.index](c)
  12. 2:00:16 PM web.1 | /home/go/src/gopkg.in/gin-gonic/gin.v1/logger.go:72 (0x4f074f)
  13. 2:00:16 PM web.1 | LoggerWithWriter.func1: c.Next()
  14. 2:00:16 PM web.1 | /home/go/src/gopkg.in/gin-gonic/gin.v1/context.go:97 (0x4e187a)
  15. 2:00:16 PM web.1 | (*Context).Next: c.handlers[c.index](c)
  16. 2:00:16 PM web.1 | /home/go/src/gopkg.in/gin-gonic/gin.v1/gin.go:284 (0x4e7c0e)
  17. 2:00:16 PM web.1 | (*Engine).handleHTTPRequest: context.Next()
  18. 2:00:16 PM web.1 | /home/go/src/gopkg.in/gin-gonic/gin.v1/gin.go:265 (0x4e74f0)
  19. 2:00:16 PM web.1 | (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
  20. 2:00:16 PM web.1 | /home/kebe/golang/go/src/net/http/server.go:2202 (0x4b249d)
  21. 2:00:16 PM web.1 | serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
  22. 2:00:16 PM web.1 | /home/kebe/golang/go/src/net/http/server.go:1579 (0x4aee07)
  23. 2:00:16 PM web.1 | (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
  24. 2:00:16 PM web.1 | /home/kebe/golang/go/src/runtime/asm_amd64.s:2086 (0x45a081)
  25. 2:00:16 PM web.1 | goexit: BYTE $0x90 // NOP
  26. 2:00:16 PM web.1 |
  27. 2:00:16 PM web.1 | [GIN] 2017/06/20 - 14:00:16 | 500 | 753.521212ms | 127.0.0.1 | GET /polls

My Code is as follows

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "gopkg.in/gin-gonic/gin.v1"
  8. "gopkg.in/mgo.v2"
  9. )
  10. type Poll struct {
  11. ID string `json:"id,omitempty"`
  12. Firstname string `json:"firstname,omitempty"`
  13. Lastname string `json:"lastname,omitempty"`
  14. Poll string `json:"poll,omitempty"`
  15. }
  16. var (
  17. // Session stores mongo session
  18. Session *mgo.Session
  19. // Mongo stores the mongodb connection string information
  20. Mongo *mgo.DialInfo
  21. )
  22. const (
  23. // MongoDBUrl is the default mongodb url that will be used to connect to the
  24. // database.
  25. MongoDBUrl = "mongodb://localhost:27017/smartpoll"
  26. // CollectionPoll holds the name of the articles collection
  27. CollectionPoll = "polls"
  28. )
  29. // Connect connects to mongodb
  30. func Connect() {
  31. uri := os.Getenv("MONGODB_URL")
  32. if len(uri) == 0 {
  33. uri = MongoDBUrl
  34. }
  35. mongo, err := mgo.ParseURL(uri)
  36. s, err := mgo.Dial(uri)
  37. if err != nil {
  38. fmt.Printf("Can't connect to mongo, go error %v\n", err)
  39. panic(err.Error())
  40. }
  41. s.SetSafe(&mgo.Safe{})
  42. fmt.Println("Connected to", uri)
  43. Session = s
  44. Mongo = mongo
  45. }
  46. func init() {
  47. Connect()
  48. }
  49. func main() {
  50. port := os.Getenv("PORT")
  51. if port == "" {
  52. log.Fatal("$PORT must be set")
  53. }
  54. router := gin.Default()
  55. router.GET("/", func (c *gin.Context) {
  56. c.JSON(http.StatusOK, gin.H{"message": "OK"})
  57. })
  58. router.GET("/polls", allPolls)
  59. router.Run(":" + port)
  60. }
  61. func allPolls(c *gin.Context) {
  62. db := c.MustGet("db").(*mgo.Database)
  63. polls := []Poll{}
  64. poll := db.C(CollectionPoll).Find(&polls)
  65. c.JSON(http.StatusOK, gin.H{
  66. "_id": "ID",
  67. "firstname": "Firstname",
  68. "lastname": "Lastname",
  69. "poll": poll,
  70. })
  71. }

My DB is as follows:

  1. /* 0 */
  2. {
  3. "_id" : ObjectId("58d9cf1cdf353f3d2f5951b4"),
  4. "id" : "1",
  5. "firstname" : "Sam",
  6. "lastname" : "Smith",
  7. "poll" : "Who is the Richest Man in the World"
  8. }

答案1

得分: 1

要从gin上下文(c)中获取数据库对象(db) c.MustGet("db").(*mgo.Database),你首先需要使用gin中间件函数进行设置。

  1. func ConnectMiddleware(c *gin.Context) {
  2. c.Set("db", Session.DB(Mongo.Database))
  3. c.Next()
  4. }

然后通过以下代码使用这个中间件:

  1. router := gin.Default()
  2. router.Use(ConnectMiddleware)
  3. router.GET("/", func(c *gin.Context) {
  4. c.JSON(http.StatusOK, gin.H{"message": "OK"})
  5. })
  6. router.GET("/polls", allPolls)

你正在使用一个过时的gin库版本,其他部分的代码都没问题。

导入最新的github.com/gin-gonic/gin包。

使用以下代码从数据库获取数据:

  1. func allPolls(c *gin.Context) {
  2. db := c.MustGet("db").(*mgo.Database)
  3. polls := []Poll{}
  4. err := db.C(CollectionPoll).Find(nil).All(&polls)
  5. if err != nil {
  6. c.Error(err)
  7. }
  8. result := gin.H{"payload": polls}
  9. c.Writer.Header().Set("Content-Type", "application/json")
  10. c.JSON(200, result)
  11. }
英文:

To get the database object (db) from the gin context (c) c.MustGet("db").(*mgo.Database) you have to first set it using a gin middleware function

  1. func ConnectMiddleware(c *gin.Context) {
  2. c.Set("db", Session.DB(Mongo.Database))
  3. c.Next()
  4. }

and then use this middleware through the following code

  1. router := gin.Default()
  2. router.Use(ConnectMiddleware)
  3. router.GET("/", func(c *gin.Context) {
  4. c.JSON(http.StatusOK, gin.H{"message": "OK"})
  5. })
  6. router.GET("/polls", allPolls)

You are using a outdated version of gin library other things are fine in your code

import the latest package form github.com/gin-gonic/gin

Use this to get the data from the database

  1. func allPolls(c *gin.Context) {
  2. db := c.MustGet("db").(*mgo.Database)
  3. polls := []Poll{}
  4. err := db.C(CollectionPoll).Find(nil).All(&polls)
  5. if err != nil {
  6. c.Error(err)
  7. }
  8. result := gin.H{"payload": polls}
  9. c.Writer.Header().Set("Content-Type", "application/json")
  10. c.JSON(200, result)
  11. }

答案2

得分: 0

在你的请求中没有名为"db"的变量。

当你执行c.MustGet("db")时,你确保有一个名为"db"的参数,但实际上并没有提供。

Gin代码会引发错误

你需要在请求中传递"name"参数。

更好的解决方案是:如果参数不存在,则返回HTTP错误400 Bad Request。

英文:

There is no variable named "db" in your request.

When you're executing c.MustGet("db") you're assure that there is a param with this name, but it isn't presented.

Gin code which panics

You need to pass "db" name in to your request.

Better solution: if it's not present return HTTP error 400 Bad request.

huangapple
  • 本文由 发表于 2017年6月20日 21:10:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/44654020.html
匿名

发表评论

匿名网友

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

确定