dsdeeaquu38538545 2017-12-04 04:21 采纳率: 100%
浏览 33

当代码在本地开发服务器中工作时,为什么GAE返回服务器错误?

I'm trying to test the Datastore functionality using Google App Engine and my code works as expected in the local development server:

// code based on the following guide: https://cloud.google.com/datastore/docs/reference/libraries#client-libraries-install-go
package datastoretest

import (
        "fmt"
        "log"
        "net/http"
        "cloud.google.com/go/datastore"
        "google.golang.org/appengine" 
)

type Task struct {
        Description string
}

func init() {
    http.HandleFunc("/", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {

    ctx := appengine.NewContext(r)

    // Set Google Cloud Platform project ID.
    projectID := "myProjectID" //note: actual ID is different

    // Creates a client.
    client, err := datastore.NewClient(ctx, projectID)
    if err != nil {
        log.Fatalf("Failed to create client: %v", err)
    }

    // Sets the kind for the new entity.
    kind := "Task"
    // Sets the name/ID for the new entity.
    name := "sampletask1"
    // Creates a Key instance.
    taskKey := datastore.NameKey(kind, name, nil)

    // Creates a Task instance.
    task := Task{
            Description: "Buy milk",
    }

    // Saves the new entity.
    if _, err := client.Put(ctx, taskKey, &task); err != nil {
            log.Fatalf("Failed to save task: %v", err)
    }

    fmt.Fprint(w, "Saved ", taskKey, ":", task.Description)

}

However, after being deployed to a GAE project, it's returning the following message to the visitor:

Error: Server Error
The server encountered an error and could not complete your request.
Please try again in 30 seconds.
  • 写回答

1条回答 默认 最新

  • doudao1282 2017-12-04 11:35
    关注

    I found out that the use of the package "cloud.google.com/go/datastore" was causing the problem. Instead, the solution should have been implemented using the package "google.golang.org/appengine/datastore". It appears that the former package was not compatible with GAE in my implementation. Switching to the latter package led to working code. For Datastore in GAE a better tutorial to follow should have been the following: https://cloud.google.com/appengine/docs/standard/go/getting-started/creating-guestbook

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧