【问题标题】:Errors Getting list of polls from mongoDB with go-gin and mgo使用 go-gin 和 mgo 从 mongoDB 获取民意调查列表时出错
【发布时间】:2017-11-23 01:18:30
【问题描述】:

您好,尝试在 Go 语言上使用 go-gin 和 mgo 构建 Web 服务,我有一个带有 mongoDB 的数据库,但每次我尝试从数据库中获取民意调查时,我都会从 Go 网络服务器的以下错误中得到错误。

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

我的代码如下

package main

import (

    "fmt"
    "log"
    "net/http"  
    "os"

    "gopkg.in/gin-gonic/gin.v1"
    "gopkg.in/mgo.v2"

)

type Poll struct {
    ID        string `json:"id,omitempty"`
    Firstname string `json:"firstname,omitempty"`
    Lastname  string `json:"lastname,omitempty"`
    Poll      string `json:"poll,omitempty"`

}

var (
    // Session stores mongo session
    Session *mgo.Session

    // Mongo stores the mongodb connection string information
    Mongo *mgo.DialInfo
)

const (
    // MongoDBUrl is the default mongodb url that will be used to connect to the
    // database.
    MongoDBUrl = "mongodb://localhost:27017/smartpoll"

        // CollectionPoll holds the name of the articles collection
    CollectionPoll = "polls"
)

// Connect connects to mongodb
func Connect() {
    uri := os.Getenv("MONGODB_URL")

    if len(uri) == 0 {
        uri = MongoDBUrl
    }

    mongo, err := mgo.ParseURL(uri)
    s, err := mgo.Dial(uri)
    if err != nil {
        fmt.Printf("Can't connect to mongo, go error %v\n", err)
        panic(err.Error())
    }
    s.SetSafe(&mgo.Safe{})
    fmt.Println("Connected to", uri)
    Session = s
    Mongo = mongo
}


func init() {
    Connect()
}

func main() {
    port := os.Getenv("PORT")

    if port == "" {
        log.Fatal("$PORT must be set")
    }



    router := gin.Default()

    router.GET("/", func (c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{"message": "OK"})

    })
    router.GET("/polls", allPolls)
    router.Run(":" + port)
}



func allPolls(c *gin.Context) {
    db := c.MustGet("db").(*mgo.Database)
    polls := []Poll{}
    poll := db.C(CollectionPoll).Find(&polls)

    c.JSON(http.StatusOK, gin.H{

        "_id": "ID",
        "firstname": "Firstname",
        "lastname": "Lastname",
        "poll": poll,
        })
}

我的数据库如下:

/* 0 */
{
    "_id" : ObjectId("58d9cf1cdf353f3d2f5951b4"),
    "id" : "1",
    "firstname" : "Sam",
    "lastname" : "Smith",
    "poll" : "Who is the Richest Man in the World"
}

【问题讨论】:

  • 我认为panic("Key \"" + key + "\" does not exist")这一行是解决问题的关键。

标签: mongodb go webserver mgo go-gin


【解决方案1】:

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

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

然后通过下面的代码使用这个中间件

router := gin.Default()
router.Use(ConnectMiddleware)
router.GET("/", func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{"message": "OK"})

})
router.GET("/polls", allPolls)

您使用的是过时版本的 gin 库,您的代码中的其他内容都很好

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

使用它从数据库中获取数据

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

【讨论】:

  • 服务器错误消失但 GET 的输出不返回数据库内容,而是我得到 dis {"_id":"ID","firstname":"Firstname","lastname":"Lastname" ,"poll":{}} 作为 local:5000/polls 的输出
  • gist.github.com/kebeeyong/fbd1c3e2ee843d2617d2ef9ef4fdca85 我在 git gist 上的更新代码,并附有您的建议。 @Aravind
  • 我已经在上面的答案中添加了您必须做的更改,请查看
  • 完美运行,非常感谢朋友
  • 嗨@Aravind 我感谢您到目前为止的帮助,我正在尝试按 id 获取帖子,但我遇到错误“无法编组字符串 ....”我的代码git gist gist.github.com/kebeeyong/cd7ba6f9bccc100e89e00a4035677fff func pollsByID
【解决方案2】:

您的请求中没有名为“db”的变量。

当您执行 c.MustGet("db") 时,您可以确保有一个具有此名称的参数,但它没有显示。

Gin code which panics

您需要将“db”名称传递给您的请求。

更好的解决方案:如果它不存在,则返回 HTTP 错误 400 Bad request。

【讨论】:

  • 请帮帮我,我刚刚将数据库更改为 smartpoll,但仍有错误
  • 我的请求是500
  • 这是你的回应(服务器告诉你什么),我问的是请求(你向服务器发送什么)
  • GET 请求显示来自数据库的投票
  • @007 你能准确地展示你要求什么/如何要求吗?你执行什么命令或代码?
猜你喜欢
  • 2017-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-20
相关资源
最近更新 更多