These are chat archives for gin-gonic/gin

5th
Jul 2017
kebeeyong
@kebeeyong
Jul 05 2017 16:09
hi
i try to query a single item from a list
n i get this error "Can't marshal string as a BSON document"
get by id n i get this
Can't marshal string as a BSON document

func pollsByID(c gin.Context) {
db := c.MustGet("db").(
mgo.Database)
id := c.Param("id")
poll := []Poll{}
err := db.C(CollectionPoll).Find(id).One(&poll)
// err := db.C(CollectionPoll).Find(bson.M{"_id": id}).One(&poll)

if err != nil {
    //c.Error(err)
    //panic(err)
    log.Println(err)
}
result := gin.H{"payload": poll}
c.Writer.Header().Set("Content-Type", "application/json")
c.JSON(200, result)

}

please help
kebeeyong
@kebeeyong
Jul 05 2017 16:14

`func pollsByID(c gin.Context) {
db := c.MustGet("db").(
mgo.Database)
id := c.Param("id")
poll := []Poll{}
err := db.C(CollectionPoll).Find(id).One(&poll)
// err := db.C(CollectionPoll).Find(bson.M{"_id": id}).One(&poll)

if err != nil {
    //c.Error(err)
    //panic(err)
    log.Println(err)
}
result := gin.H{"payload": poll}
c.Writer.Header().Set("Content-Type", "application/json")
c.JSON(200, result)

}`

Javier Provecho Fernandez
@javierprovecho
Jul 05 2017 16:38
@kebeeyong can you post the struct Poll?
Luca Moser
@luca-moser
Jul 05 2017 17:53
Hello everyone.
If you ctx.Error(err) and return immediately, will other middleware still be called?
Javier Provecho Fernandez
@javierprovecho
Jul 05 2017 18:32
@luca-moser actually yes, unless you run c.abort
Also there is the possibility that a middle ware has run c.next() for post processing
If a handler calls c.abort inside a c.next, the c.next will still return
And that middle ware will be able to check if context has been cancelled
Luca Moser
@luca-moser
Jul 05 2017 19:46
my implementation seems to be working fortunately
I can simply ctx.Error(err) inside handlers and a single error handler middleware does the correct thing with the error.
without having to pollute the code with a lot of http.AbortXXXX
thanks for the info @javierprovecho