Template Parsing Error! [SOLVED]

So I have this template

{{define content}}

<div id="second" class="row scrollspy" style="margin-top:20px; margin-bottom:10px;">
  <div class="card light-green darken-2 z-depth-4">
    <div class="card-content white" style="opacity:0.8;">
      <span class="card-title">{{if not .Name}}No Name{{else}}{{.Name}}{{end}}'s Organizations</span>
<table id="table" class="highlight">
  <thead>
  <tr>
    <th>Name</th><th>Address</th><th>Type</th>
  </tr>
</thead>
<tbody>
  {{if not .Organizations }}
  <h1>User has No Organizations</h1>
  {{else}}
  {{range .Organizations}}
  <tr id="">
    <td>{{if not .Name}}No Name{{else}}{{.Name}}{{end}}</td><td>{{if not .Address}}No Address{{else}}{{.Address.Street}}{{end}}</td><td>{{if not .Type}}No type{{else}}{{.Type}}{{end}}</td>
  </tr>
  {{end}}
  {{end}}
</tbody>
</table>
<a class="waves-effect waves-light btn" href="/dashboard/my/organizations/new/"><i class="material-icons right">add</i>Add New</a>
</div>
</div>
</div>
{{end}}

The function that feeds this template its values is:

// OrganizationMainDash http handler serves Organization Dashboard portal
func OrganizationMainDash(w http.ResponseWriter, r *http.Request) {
	currentUser := r.Context().Value("currentUser")

	if currentUser == "guest" {
		http.Error(w, "Attempted to access restricted content without logging in a user", http.StatusUnauthorized)
	} else {

		type Page struct {
			Name          string
			Organizations []orgs.Organization
		}

		pageStruct := Page{}

		db, err := gorm.Open(os.Getenv("DataEngine"), os.Getenv("DATABASE_URL"))
		if err != nil {
			http.Error(w, "Failed to Connect to Database!", http.StatusInternalServerError)
		}
		defer db.Close()
		userID, err := session.GetInt(r, "userID")
		if err != nil {
			http.Error(w, "No session exists!", http.StatusInternalServerError)
		}
		req := get.Request{UserID: uint(userID)}
		res, err := get.OrgsByUserID(db, req)
		if err != nil {
			http.Error(w, "Failed to Retrieve Organizations!", http.StatusInternalServerError)
		}
		for i, _ := range res.Organizations {
			pageStruct.Organizations = append(pageStruct.Organizations, res.Organizations[i])
		}
		db.Table("users").Select("name").Where("id = $1", uint(userID)).Scan(&pageStruct)
		if pageStruct.Name == "" {
			pageStruct.Name = "Unnamed User!"
		}
		log.Printf("Value of Page struct: %v\n", pageStruct)
		t, _ := template.ParseFiles("Templates/contentlayout.html", "Templates/privatenav.html", "Templates/dashboardorglist.html")
		t.Execute(w, pageStruct)
	}
}

The error message that Im getting from Heroku logs is:

2017-10-30T13:18:39.112072+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/dashboard/my/organizations/" host=vas.herokuapp.com request_id=ecccef03-933e-42b4-8dd3-8a1e8cb60d5c fwd="45.43.97.233" dyno=web.1 connect=0ms service=29ms status=503 bytes=0 protocol=https
2017-10-30T13:18:39.110527+00:00 app[web.1]: 2017/10/30 13:18:39 Value of Page struct: {Unnamed User! [{{42 2017-10-26 20:58:10.908221 +0000 UTC 2017-10-26 20:58:10.908221 +0000 UTC <nil>} Curt's New Practice Descriptions of organization! {{0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>}    0 0 0} CurtPractice@email.com 555-555-5555  1 [] [] []} {{43 2017-10-26 20:58:10.997191 +0000 UTC 2017-10-26 20:58:10.997191 +0000 UTC <nil>} Curt's New Practice Descriptions of organization! {{0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>}    0 0 0} CurtPractice@email.com 555-555-5555  1 [] [] []}]}
2017-10-30T13:18:39.111922+00:00 app[web.1]: 2017/10/30 13:18:39 http: panic serving 10.37.186.47:21553: runtime error: invalid memory address or nil pointer dereference
2017-10-30T13:18:39.111924+00:00 app[web.1]: goroutine 158 [running]:
2017-10-30T13:18:39.111925+00:00 app[web.1]: net/http.(*conn).serve.func1(0xc4201b63c0)
2017-10-30T13:18:39.111926+00:00 app[web.1]:    /app/tmp/cache/go1.9.2/go/src/net/http/server.go:1697 +0xd0
2017-10-30T13:18:39.111927+00:00 app[web.1]: panic(0x8510e0, 0xb35560)
2017-10-30T13:18:39.111927+00:00 app[web.1]:    /app/tmp/cache/go1.9.2/go/src/runtime/panic.go:491 +0x283
2017-10-30T13:18:39.111928+00:00 app[web.1]: html/template.(*Template).escape(0x0, 0x0, 0x0)
2017-10-30T13:18:39.111929+00:00 app[web.1]:    /app/tmp/cache/go1.9.2/go/src/html/template/template.go:95 +0x38
2017-10-30T13:18:39.111929+00:00 app[web.1]: html/template.(*Template).Execute(0x0, 0x7fec89614c30, 0xc4201ca380, 0x869960, 0xc42095e3f0, 0xc4207de790, 0xc4202333b0)
2017-10-30T13:18:39.111930+00:00 app[web.1]:    /app/tmp/cache/go1.9.2/go/src/html/template/template.go:119 +0x2f
2017-10-30T13:18:39.111941+00:00 app[web.1]: bitbucket.org/yyhmsg/myacume/dashboard.OrganizationMainDash(0xb084e0, 0xc4201ca380, 0xc420816400)
2017-10-30T13:18:39.111943+00:00 app[web.1]:    /tmp/tmp.fFH9PyTgI8/.go/src/bitbucket.org/yyhmsg/myacume/dashboard/serve.go:73 +0x7b3
2017-10-30T13:18:39.111944+00:00 app[web.1]: net/http.HandlerFunc.ServeHTTP(0x8eade0, 0xb084e0, 0xc4201ca380, 0xc420816400)
2017-10-30T13:18:39.111945+00:00 app[web.1]:    /app/tmp/cache/go1.9.2/go/src/net/http/server.go:1918 +0x44
2017-10-30T13:18:39.111946+00:00 app[web.1]: bitbucket.org/yyhmsg/myacume/vendor/github.com/gorilla/mux.(*Router).ServeHTTP(0xc420203810, 0xb084e0, 0xc4201ca380, 0xc420816400)
2017-10-30T13:18:39.111946+00:00 app[web.1]:    /tmp/tmp.fFH9PyTgI8/.go/src/bitbucket.org/yyhmsg/myacume/vendor/github.com/gorilla/mux/mux.go:114 +0xdc
2017-10-30T13:18:39.111947+00:00 app[web.1]: bitbucket.org/yyhmsg/myacume/middleware.CSRFServe.func1(0xb084e0, 0xc4201ca380, 0xc420816200)
2017-10-30T13:18:39.111947+00:00 app[web.1]:    /tmp/tmp.fFH9PyTgI8/.go/src/bitbucket.org/yyhmsg/myacume/middleware/csrf.go:22 +0x353
2017-10-30T13:18:39.111948+00:00 app[web.1]: net/http.HandlerFunc.ServeHTTP(0xc4202d8100, 0xb084e0, 0xc4201ca380, 0xc420816200)
2017-10-30T13:18:39.111949+00:00 app[web.1]:    /app/tmp/cache/go1.9.2/go/src/net/http/server.go:1918 +0x44
2017-10-30T13:18:39.111949+00:00 app[web.1]: bitbucket.org/yyhmsg/myacume/middleware.AuthServe.func1(0xb084e0, 0xc4201ca380, 0xc420816100)
2017-10-30T13:18:39.111950+00:00 app[web.1]:    /tmp/tmp.fFH9PyTgI8/.go/src/bitbucket.org/yyhmsg/myacume/middleware/auth.go:20 +0x265
2017-10-30T13:18:39.111951+00:00 app[web.1]: net/http.HandlerFunc.ServeHTTP(0xc4202d8120, 0xb084e0, 0xc4201ca380, 0xc420816100)
2017-10-30T13:18:39.111951+00:00 app[web.1]:    /app/tmp/cache/go1.9.2/go/src/net/http/server.go:1918 +0x44
2017-10-30T13:18:39.111952+00:00 app[web.1]: bitbucket.org/yyhmsg/myacume/vendor/github.com/alexedwards/scs/session.(*manager).ServeHTTP(0xc4201f8300, 0x7fec89614bf8, 0xc4201971a0, 0xc420816000)
2017-10-30T13:18:39.111953+00:00 app[web.1]:    /tmp/tmp.fFH9PyTgI8/.go/src/bitbucket.org/yyhmsg/myacume/vendor/github.com/alexedwards/scs/session/manager.go:66 +0xda
2017-10-30T13:18:39.111954+00:00 app[web.1]: bitbucket.org/yyhmsg/myacume/vendor/github.com/gorilla/handlers.loggingHandler.ServeHTTP(0xb042a0, 0xc42000e018, 0xb02ee0, 0xc4201f8300, 0xb08a60, 0xc4207aa0e0, 0xc420816000)
2017-10-30T13:18:39.111954+00:00 app[web.1]:    /tmp/tmp.fFH9PyTgI8/.go/src/bitbucket.org/yyhmsg/myacume/vendor/github.com/gorilla/handlers/handlers.go:69 +0x123
2017-10-30T13:18:39.111955+00:00 app[web.1]: bitbucket.org/yyhmsg/myacume/vendor/github.com/gorilla/handlers.(*loggingHandler).ServeHTTP(0xc4202d8140, 0xb08a60, 0xc4207aa0e0, 0xc420816000)
2017-10-30T13:18:39.111956+00:00 app[web.1]:    <autogenerated>:1 +0x75
2017-10-30T13:18:39.111957+00:00 app[web.1]: net/http.serverHandler.ServeHTTP(0xc42011e000, 0xb08a60, 0xc4207aa0e0, 0xc420816000)
2017-10-30T13:18:39.111957+00:00 app[web.1]:    /app/tmp/cache/go1.9.2/go/src/net/http/server.go:2619 +0xb4
2017-10-30T13:18:39.111958+00:00 app[web.1]: net/http.(*conn).serve(0xc4201b63c0, 0xb091a0, 0xc4201c4180)
2017-10-30T13:18:39.111958+00:00 app[web.1]:    /app/tmp/cache/go1.9.2/go/src/net/http/server.go:1801 +0x71d
2017-10-30T13:18:39.111959+00:00 app[web.1]: created by net/http.(*Server).Serve

I’m beating my head against this and can’t figure out why I keep getting nil pointer dereference, any help would be appreciated.

1 Like

Perhaps inspecting the omitted error value might give you some hints?


edit

Quickly replicating your example at play.golang.org (Go Playground - The Go Programming Language) gives me the following error:

template: foo:1: unexpected "content" in define clause

Then looking up the documentation of define, tells me that the name needs to be in quotes, changing content to "content" in your example does at least parse the template.

So, errors are returned for a reason…

1 Like

Thank you so much for that. I will be certain to check errors for everything no matter what from here on, because this cost me a ton of time over something so ridiculously trivial!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.