panic: not an App Engine context

1,835 views
Skip to first unread message

prakas...@gmail.com

unread,
Oct 5, 2017, 1:05:59 PM10/5/17
to google-appengine-go
Hi,

I'm getting this error while running the application in my local system, code is below. Some one can help me to resolve this error.

panic: not an App Engine context

datastore.go

package main

import (
"log"
"net/http"
"os"
"encoding/json"

"cloud.google.com/go/datastore"

"golang.org/x/net/context"

"google.golang.org/appengine"
)

var datastoreClient *datastore.Client

func main() {
ctx := context.Background()

// Set this in app.yaml when running in production.
projectID := os.Getenv("GCLOUD_DATASET_ID")

var err error
datastoreClient, err = datastore.NewClient(ctx, projectID)
if err != nil {
log.Fatal(err)
}

http.HandleFunc("/", articlesList)
appengine.Main()
}


type Article struct {
ID int64
Title string
Content string
}
func articlesList(res http.ResponseWriter, req *http.Request) {
ctx := context.Background()
query := datastore.NewQuery("Article").
Order("Title").
Limit(10)

articles := make([]*Article, 0)
_, err := datastoreClient.GetAll(ctx, query, &articles)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(res).Encode(articles)
}

app.yaml


runtime: go
api_version: go1

handlers:
- url: /.*
script: _go_app

#env: flex

#automatic_scaling:
# min_num_instances: 1

#[START env_variables]
env_variables:
GCLOUD_DATASET_ID: <ProjectId>
#[END env_variables]

pj

unread,
Oct 6, 2017, 7:39:45 AM10/6/17
to google-appengine-go
You can't use context.Background()

You have to use newcontext(r) from app engine package

Prakash Natarajan

unread,
Oct 15, 2017, 6:21:30 AM10/15/17
to pj, google-appengine-go
Hi Pj,

    Thanks for your help.




Thanks,
Prakash.N

On Fri, Oct 6, 2017 at 5:09 PM, pj <p...@skylove.ly> wrote:
You can't use context.Background()

You have to use newcontext(r) from app engine package

--
You received this message because you are subscribed to a topic in the Google Groups "google-appengine-go" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-appengine-go/i07tIM3ACAk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-appengine-go+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine-go/390438a6-96f6-407b-a3d9-8453c356e23d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages