为了账号安全,请及时绑定邮箱和手机立即绑定

恐慌 - 未收到数据 - ERR_EMPTY_RESPONSE

恐慌 - 未收到数据 - ERR_EMPTY_RESPONSE

Go
qq_遁去的一_1 2021-12-27 10:38:03
我正在使用gorethink驱动程序,我在模型中编写了这样的查询函数func GetQuotesByUser(idUser string) []Quote{    ids:=GetQuoteIdsByUser(idUser)    if (len(ids)>0){        result,err:=r.Table("quote").GetAll(ids...).Run(config.Connection())        defer result.Close()        if err!=nil{            fmt.Println(err)            return []Quote{}        }        var quotes []Quote        err=result.All(&quotes)        if err!=nil{            fmt.Println(err)            return []Quote{}        }        fmt.Println(quotes)        return quotes    } else{        return []Quote{}    }}我编写了一个调用上述函数的路由处理程序import (    "corate/util"    "corate/model"    "github.com/fatih/structs"    "net/http")func DashboardHandler(w http.ResponseWriter,r *http.Request){    files:=[]string{"base","dashboard"}    session,_:=util.GlobalSessions.SessionStart(w,r)    defer session.SessionRelease(w)    quotes:=model.GetQuotesByUser("ca8a2e14-f65b-43b1-b655-97d7c29190ec")    q:=structs.Map(quotes)    util.RenderTemplate(w,q,files...)}这是main功能func main(){    // Setup database    config.Setupdb()    // Initialize session manager    util.InitSessionManager()    // Serve static folder    http.Handle("/public/",http.StripPrefix("/public/",http.FileServer(http.Dir("public"))))    // Setup routers and middlewares    http.HandleFunc("/",route.IndexHandler)    http.HandleFunc("/login",route.GoogleLoginHandler)    http.HandleFunc("/auth/google",route.GoogleCallbackHandler)    http.Handle("/dashboard",negroni.New(        negroni.HandlerFunc(route.IsAuthenticated),        negroni.Wrap(http.HandlerFunc(route.DashboardHandler)),    ))    // Start listening    http.ListenAndServe(":3000",nil)}我使用astaxie/beego/session库来管理此服务器的会话import (    "github.com/astaxie/beego/session")var (    GlobalSessions *session.Manager)当我启动服务器,登录并请求dashboard路径,DashboardHandler被处理,GetQuotesByUser被调用,它工作,它工作,它确实打印出查询的真实结果。但是在浏览器中,浏览器抛出错误:没有接收到数据ERR_EMPTY_RESPONSE为什么会发生?然后我注意到,如果我不使用fatih/structs库,就不会出错。但是我想使用那个库,它对我有很大帮助。我想知道这种奇怪行为的原因以及如何在仍在使用fatih/structs库的同时修复它
查看完整描述

1 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

GetQuotesByUser返回一个切片,而不是一个结构。所以你不能直接将它传递给structs.Map. 您必须迭代切片并将每个元素单独转换为地图。


quotes := model.GetQuotesByUser("ca8a2e14-f65b-43b1-b655-97d7c29190ec")


qs := []map[string]interface{}{}

for _, q := range quotes {

    qs = append(qs, structs.Map(q))

}


util.RenderTemplate(w,qs,files...)

应该让你接近你所追求的。


查看完整回答
反对 回复 2021-12-27
  • 1 回答
  • 0 关注
  • 242 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号