无法评估界面中的字段

时间:2019-03-30 18:30:27

标签: go

我正在尝试使用另一个集合中的数据从mongo集合中获取数据。但是它拒绝工作,因为它返回http: panic serving [::1]:51494: template: display.html:27:27: executing "builds.display" at <.TalentData>: can't evaluate field TalentData in type buildcontroller.Build

type Build struct {
    Id bson.ObjectId `bson:"_id,omitempty"`
    Name string `bson: "name,omitempty"` 
    CreatedAt *time.Time `bson: "createdAt,omitempty"` 
    UpdatedAt *time.Time `bson: "updatedAt,omitempty"` 
    Author bson.ObjectId `bson: "author,omitempty"` 
    Class bson.ObjectId `bson: "class,omitempty"` 
    Talents []int64 `bson: "talents,omitempty"`
}

type Talents struct {
    Id bson.ObjectId `bson:"_id,omitempty"`
    Name string `bson: "name,omitempty"` 
    Description string `bson: "description,omitempty"` 
    Class bson.ObjectId `bson: "class,omitempty"` 
}

type IndexData struct {
    PageTitle       string
    BuildData       interface{}
    TalentData      interface{}
}

func Display(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)

    if !bson.IsObjectIdHex(vars["id"]) {
        err := template.NotFoundTemplate.ExecuteTemplate(w, "skeleton", IndexData{PageTitle: "Page Not Found"})
        if err != nil {
            log.Panic(err)
        }

        return
    }

    session := database.GetMongoSession()
    defer session.Close()

    c := session.DB(config.Settings.Database.Name).C("builds")
    var result []Build
    var class bson.ObjectId

    err := c.Find(bson.M{"_id": bson.ObjectIdHex(vars["id"])}).All(&result)
    if err != nil {
        w.WriteHeader(http.StatusNotFound)
    }

    for _, v := range result {
        class = v.Class
    }

    talents, err := getTalents(class)
    if err != nil {
        log.Fatal(err)
    }

    data := IndexData{
        PageTitle:       "test",
        BuildData:       result,
        TalentData:      talents,
    }

    err = template.BuildDisplayTemplate.ExecuteTemplate(w, "skeleton", data)
    if err != nil {
        log.Panic(err)
    }
}

func getTalents(class bson.ObjectId) (interface{}, error) {
    session := database.GetMongoSession()
    defer session.Close()

    c := session.DB(config.Settings.Database.Name).C("talents")
    var result []Talents

    err := c.Find(bson.M{"class": class}).All(&result)
    if err != nil {
        return nil, err
    }

    return result, nil
}

我觉得这很奇怪,因为resulttalents应该以相同的方式返回格式化的数据,并且result确实可以正常工作。

{{ range $data := .BuildData }} <- This works
    {{ $data.Name }}
    {{ range $talent := .TalentData }} <- Nope.
        {{$talent.Description}}
    {{ end }}
{{ end }}

我已经比较了两个变量的输出,并且正如我所说,它们实际上是以相同的方式格式化的吗?

talents = [{ObjectIdHex("5c9fa88fe86deef65491c31e") Test Something ObjectIdHex("5c9fa8cfe86deef65491c31f")} {ObjectIdHex("5c9faa68cb216df65405dc5b") Hello Something ObjectIdHex("5c9fa8cfe86deef65491c31f")}]

result = [{ObjectIdHex("5c9f89bde86deef65491c31d") Test <nil> <nil> ObjectIdHex("5c9faa10e86deef65491c320") ObjectIdHex("5c9fa8cfe86deef65491c31f") []}]

我在这里想念什么?

0 个答案:

没有答案