困っていること
Google App Engineのサンプルを参考に、JSON
をPOST
することでDataStore
にPUT
されるサンプルを作りたいのですが、どうしてもdatastore: invalid entity type
が出てしまいうまくいきません。
全取得であるGetAll
はJSON
をDataStore
から返してくれており、動いています。。
コードです
package main import ( "fmt" "github.com/labstack/echo" "github.com/labstack/echo/middleware" "google.golang.org/appengine" "google.golang.org/appengine/datastore" "google.golang.org/appengine/log" "net/http" "time" ) var e = createMux() type Post struct { Author string `json:"author"` Message string `json:"message"` Posted time.Time `json:"posted"` } type templateParams struct { Notice string `json:"notice"` Name string `json:"name"` Message string `json:"message"` Posts []Post `json:"posts"` } func main() { e := echo.New() e.Start(":8080") appengine.Main() } func createMux() *echo.Echo { e := echo.New() http.Handle("/", e) return e } func init() { g := e.Group("/posts") g.Use(middleware.CORS()) g.POST("", createPost) g.GET("", getPosts) } // ???? 動かない部分です func createPost(c echo.Context) error { ctx := appengine.NewContext(c.Request()) post := new(Post) if err := c.Bind(post); err != nil { return err } post.Posted = time.Now() key := datastore.NewIncompleteKey(ctx, "Post", nil) if _, err := datastore.Put(ctx, key, &post); err != nil { log.Errorf(ctx, "datastore.Put: %v", err) c.Response().WriteHeader(http.StatusInternalServerError) params := templateParams{} params.Notice = "Postを格納できませんでした。" + err.Error() return c.JSON(http.StatusInternalServerError, params) // ???? datastore: invalid entity type } params := templateParams{} params.Notice = "Postを格納できました" return c.JSON(http.StatusOK, params) } // ???? 動きます func getPosts(c echo.Context) error { ctx := appengine.NewContext(c.Request()) q := datastore.NewQuery("Post").Order("-Posted").Limit(20) params := templateParams{} if _, err := q.GetAll(ctx, ¶ms.Posts); err != nil { log.Errorf(ctx, "posts取得エラー: %v", err) c.Response().WriteHeader(http.StatusInternalServerError) params.Notice = "posts取得エラー発生しました" return c.JSON(http.StatusInternalServerError, params) } params.Notice = fmt.Sprintf("取得成功した") return c.JSON(http.StatusOK, params.Posts) }
デプロイ
$ gcloud app deploy
リクエストの出し方
POSTMANでURLに
{"author":"hoge_author","message":"hoge_message"}
のようなJSON
をPOST
しています。datastore: invalid entity type
でハマったことがある方、是非教えていただけると幸いです。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/09/14 07:00