私はGoogle App Engineのを使用してデータストアの機能をテストしようとしていると、ローカルの開発サーバーで期待どおりに私のコードは動作します:コードがローカル開発サーバーで動作するとき、なぜGAEはサーバーエラーを返すのですか?
// 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)
}
しかし、GAEプロジェクトに配備された後、それが次のメッセージを返します訪問者:
Error: Server Error
The server encountered an error and could not complete your request.
Please try again in 30 seconds.
このエラーは、アプリケーションが終了したことを通知しています。 log.Fatalfの呼び出しのために、おそらくアプリケーションが終了しました。 [appengine logger](https://cloud.google.com/appengine/docs/standard/go/logs/reference)を使用してエラーをログに記録し、log.Fatalfを呼び出す代わりにハンドラから戻ります。コンソールのエラーを見て、何が間違っているのかを判断してください。 –
私はappengine loggerを使用して通常のログパッケージを削除しましたが、log.Fatalfを削除しても問題は解決しませんでした。関数client.Putが問題の原因であるように見えますが、コメントアウトするとウェブページが正常に読み込まれますが、なぜですか? – user
Putから返されたエラーは何ですか? –