質問編集履歴
5
コードの修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -110,16 +110,19 @@
|
|
110
110
|
db, err := gorm.Open("mysql", "kirohi:cSbuS2mlGo##@/subsc?charset=utf8&parseTime=True&loc=Local")
|
111
111
|
CheckConnectError(err)
|
112
112
|
defer db.Close()
|
113
|
-
user := model.User{
|
113
|
+
user := &model.User{
|
114
|
+
Id: model.Seq,
|
115
|
+
}
|
114
116
|
if err := c.Bind(&user); err != nil {
|
115
117
|
return err
|
116
118
|
}
|
117
119
|
|
120
|
+
model.Users[user.Id] = user
|
121
|
+
model.Seq++
|
118
122
|
db.Create(&user)
|
119
|
-
return c.
|
123
|
+
return c.JSON(http.StatusOK, user)
|
120
124
|
}
|
121
125
|
}
|
122
|
-
|
123
126
|
func FindUserById() echo.HandlerFunc {
|
124
127
|
return func(c echo.Context) (err error) {
|
125
128
|
db, err := gorm.Open("mysql", "kirohi:cSbuS2mlGo##@/subsc?charset=utf8&parseTime=True&loc=Local")
|
@@ -135,29 +138,25 @@
|
|
135
138
|
}
|
136
139
|
|
137
140
|
func UpdateUser() echo.HandlerFunc {
|
138
|
-
// emailをUpdateする処理
|
139
141
|
return func(c echo.Context) (err error) {
|
140
142
|
db, err := gorm.Open("mysql", "kirohi:cSbuS2mlGo##@/subsc?charset=utf8&parseTime=True&loc=Local")
|
141
143
|
CheckConnectError(err)
|
142
144
|
defer db.Close()
|
143
145
|
|
144
|
-
user :=
|
146
|
+
user := model.User{}
|
145
|
-
if err
|
147
|
+
if err = c.Bind(user); err != nil {
|
146
|
-
return
|
148
|
+
return
|
147
149
|
}
|
148
150
|
id, _ := strconv.Atoi(c.Param("id"))
|
149
|
-
|
151
|
+
db.Where("Id=?", id).Find(&user)
|
150
|
-
|
152
|
+
|
151
|
-
// ex.Name = user.Name
|
152
|
-
|
153
|
+
user.Name = c.Param("name")
|
153
|
-
|
154
|
+
|
154
|
-
// if err := c.Bind(user); err != nil {
|
155
|
-
|
155
|
+
fmt.Println(user.Name)
|
156
|
-
// }
|
157
|
-
// //
|
156
|
+
// //model.Users[id].Name = "あああ"
|
158
|
-
// //
|
157
|
+
// //fmt.Println(model.Users[id])
|
159
|
-
|
158
|
+
// return c.JSON(http.StatusOK, user)
|
160
|
-
return c.JSON(http.StatusOK,
|
159
|
+
return c.JSON(http.StatusOK, "ok")
|
161
160
|
}
|
162
161
|
}
|
163
162
|
|
4
誤字
title
CHANGED
File without changes
|
body
CHANGED
@@ -21,7 +21,6 @@
|
|
21
21
|
|
22
22
|
```go
|
23
23
|
func UpdateUser() echo.HandlerFunc {
|
24
|
-
// emailをUpdateする処理
|
25
24
|
return func(c echo.Context) (err error) {
|
26
25
|
db, err := gorm.Open("mysql", "mysql設定")
|
27
26
|
CheckConnectError(err) //エラーハンドリングの関数
|
3
api/user.go jsonについての追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -80,4 +80,123 @@
|
|
80
80
|
|
81
81
|
``` log
|
82
82
|
{"time":"2019-07-26T15:37:53.480874+09:00","level":"-","prefix":"echo","file":"recover.go","line":"73","message":"[PANIC RECOVER] runtime error: invalid memory address or nil pointer dereference goroutine 50 [running]:\necho/middleware.RecoverWithConfig.func1.1.1(0x156b148, 0x1000, 0xc000060000, 0x160b140, 0xc0002b2120)\n\t/Users/kirohi/go/src/echo/middleware/recover.go:71 +0xf1\npanic(0x14cc0e0, 0x195b440)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/runtime/panic.go:522 +0x1b5\ngithub.com/ia17011/subsc/backend/api.UpdateUser.func1(0x160b140, 0xc0002b2120, 0x0, 0x0)\n\t/Users/kirohi/go/src/github.com/ia17011/subsc/backend/api/user.go:74 +0x19d\ngithub.com/labstack/echo.(*Echo).add.func1(0x160b140, 0xc0002b2120, 0xc0002160a0, 0xf)\n\t/Users/kirohi/go/src/github.com/labstack/echo/echo.go:505 +0x87\ngithub.com/ia17011/subsc/backend/api/middlewares.serverHeader.func1(0x160b140, 0xc0002b2120, 0x156b148, 0x1000)\n\t/Users/kirohi/go/src/github.com/ia17011/subsc/backend/api/middlewares/mainMiddlewares.go:25 +0x235\necho/middleware.RecoverWithConfig.func1.1(0x160b140, 0xc0002b2120, 0x0, 0x0)\n\t/Users/kirohi/go/src/echo/middleware/recover.go:78 +0xd5\necho/middleware.LoggerWithConfig.func2.1(0x160b140, 0xc0002b2120, 0x3, 0x4)\n\t/Users/kirohi/go/src/echo/middleware/logger.go:119 +0x1c0\ngithub.com/labstack/echo.(*Echo).ServeHTTP(0xc00028a000, 0x15fe860, 0xc0002bc000, 0xc0001e8a00)\n\t/Users/kirohi/go/src/github.com/labstack/echo/echo.go:616 +0x22d\nnet/http.serverHandler.ServeHTTP(0xc000226750, 0x15fe860, 0xc0002bc000, 0xc0001e8a00)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/net/http/server.go:2774 +0xa8\nnet/http.(*conn).serve(0xc00021e0a0, 0x15ff560, 0xc0001ecc00)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/net/http/server.go:1878 +0x851\ncreated by net/http.(*Server).Serve\n\t/usr/local/Cellar/go/1.12.5/libexec/src/net/http/server.go:2884 +0x2f4\n\ngoroutine 1 [IO wait]:\ninternal/poll.runtime_pollWait(0x1e91ea8, 0x72, 0x0)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/runtime/netpoll.go:182 +0x56\ninternal/poll.(*pollDesc).wait(0xc000232818, 0x72, 0x0, 0x0, 0x154be4a)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/internal/poll/fd_poll_runtime.go:87 +0x9b\ninternal/poll.(*pollDesc).waitRead(...)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/internal/poll/fd_poll_runtime.go:92\ninternal/poll.(*FD).Accept(0xc000232800, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/internal/poll/fd_unix.go:384 +0x1ba\nnet.(*netFD).accept(0xc000232800, 0x30, 0x50, 0x50)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/net/fd_unix.go:238 +0x42\nnet.(*TCPListener).accept(0xc0002222f8, 0xc0f05880cb, 0xc000088000, 0xc00010dd00)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/net/tcpsock_posix.go:139 +0x32\nnet.(*TCPListener).AcceptTCP(0xc0002222f8, 0x100000001, 0xc00010dd30, 0x104c368)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/net/tcpsock.go:247 +0x48\ngithub.com/labstack/echo.tcpKeepAliveListener.Accept(0xc0002222f8, 0xc00010dd58, 0x10afda6, 0x5d3a9fc1, 0x1036bcf)\n\t/Users/kirohi/go/src/github.com/labstack/echo/echo.go:820 +0x2f\nnet/http.(*Server).Serve(0xc000226750, 0x15fe4e0, 0xc000222300, 0x0, 0x0)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/net/http/server.go:2859 +0x22d\ngithub.com/labstack/echo.(*Echo).StartServer(0xc00028a000, 0xc000226750, 0x0, 0x0)\n\t/Users/kirohi/go/src/github.com/labstack/echo/echo.go:707 +0x388\ngithub.com/labstack/echo.(*Echo).Start(...)\n\t/Users/kirohi/go/src/github.com/labstack/echo/echo.go:627\nmain.main()\n\t/Users/kirohi/go/src/github.com/ia17011/subsc/backend/main.go:16 +0x67\n\ngoroutine 51 [runnable]:\ndatabase/sql.(*DB).connectionOpener(0xc0002ca000, 0x15ff560, 0xc000220300)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/database/sql/sql.go:1000 +0xe8\ncreated by database/sql.OpenDB\n\t/usr/local/Cellar/go/1.12.5/libexec/src/database/sql/sql.go:670 +0x15e\n\ngoroutine 35 [runnable]:\ngithub.com/go-sql-driver/mysql.(*mysqlConn).startWatcher.func1(0xc0001f9080, 0xc0002c6120, 0xc0001f62a0)\n\t/Users/kirohi/go/src/github.com/go-sql-driver/mysql/connection.go:619 +0xbf\ncreated by github.com/go-sql-driver/mysql.(*mysqlConn).startWatcher\n\t/Users/kirohi/go/src/github.com/go-sql-driver/mysql/connection.go:616 +0xbe\n\ngoroutine 52 [runnable]:\ndatabase/sql.(*DB).connectionResetter(0xc0002ca000, 0x15ff560, 0xc000220300)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/database/sql/sql.go:1013 +0x\n"}
|
83
|
-
```
|
83
|
+
```
|
84
|
+
|
85
|
+
## api/user.goと送信しているjsonの追記
|
86
|
+
```go
|
87
|
+
package api
|
88
|
+
|
89
|
+
import (
|
90
|
+
"fmt"
|
91
|
+
"net/http"
|
92
|
+
"strconv"
|
93
|
+
|
94
|
+
"github.com/ia17011/subsc/backend/model"
|
95
|
+
"github.com/jinzhu/gorm"
|
96
|
+
"github.com/labstack/echo"
|
97
|
+
uuid "github.com/satori/go.uuid"
|
98
|
+
)
|
99
|
+
|
100
|
+
func getUUID() string {
|
101
|
+
u, err := uuid.NewV4()
|
102
|
+
if err != nil {
|
103
|
+
fmt.Printf("Something went wrong: %s", err)
|
104
|
+
}
|
105
|
+
uu := u.String()
|
106
|
+
return uu
|
107
|
+
}
|
108
|
+
|
109
|
+
func CreateUser() echo.HandlerFunc {
|
110
|
+
return func(c echo.Context) (err error) {
|
111
|
+
db, err := gorm.Open("mysql", "kirohi:cSbuS2mlGo##@/subsc?charset=utf8&parseTime=True&loc=Local")
|
112
|
+
CheckConnectError(err)
|
113
|
+
defer db.Close()
|
114
|
+
user := model.User{}
|
115
|
+
if err := c.Bind(&user); err != nil {
|
116
|
+
return err
|
117
|
+
}
|
118
|
+
|
119
|
+
db.Create(&user)
|
120
|
+
return c.String(http.StatusOK, "OK")
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
func FindUserById() echo.HandlerFunc {
|
125
|
+
return func(c echo.Context) (err error) {
|
126
|
+
db, err := gorm.Open("mysql", "kirohi:cSbuS2mlGo##@/subsc?charset=utf8&parseTime=True&loc=Local")
|
127
|
+
CheckConnectError(err)
|
128
|
+
defer db.Close()
|
129
|
+
user := model.User{}
|
130
|
+
|
131
|
+
id := c.Param("id")
|
132
|
+
db.First(&user, id)
|
133
|
+
|
134
|
+
return c.JSON(http.StatusOK, user)
|
135
|
+
}
|
136
|
+
}
|
137
|
+
|
138
|
+
func UpdateUser() echo.HandlerFunc {
|
139
|
+
// emailをUpdateする処理
|
140
|
+
return func(c echo.Context) (err error) {
|
141
|
+
db, err := gorm.Open("mysql", "kirohi:cSbuS2mlGo##@/subsc?charset=utf8&parseTime=True&loc=Local")
|
142
|
+
CheckConnectError(err)
|
143
|
+
defer db.Close()
|
144
|
+
|
145
|
+
user := new(model.User)
|
146
|
+
if err := c.Bind(user); err != nil {
|
147
|
+
return err
|
148
|
+
}
|
149
|
+
id, _ := strconv.Atoi(c.Param("id"))
|
150
|
+
// db.Where("id=?", id).Find(&user)
|
151
|
+
// ex := model.Users[id]
|
152
|
+
// ex.Name = user.Name
|
153
|
+
// db.Update(&user)
|
154
|
+
// user := new(model.User)
|
155
|
+
// if err := c.Bind(user); err != nil {
|
156
|
+
// return err
|
157
|
+
// }
|
158
|
+
// // model.Users[id].Name = user.Name
|
159
|
+
// // db.Where("id", id).Find(&user)
|
160
|
+
model.Users[id].Name = user.Name
|
161
|
+
return c.JSON(http.StatusOK, model.Users[id])
|
162
|
+
}
|
163
|
+
}
|
164
|
+
|
165
|
+
func DeleteUser() echo.HandlerFunc {
|
166
|
+
return func(c echo.Context) (err error) {
|
167
|
+
db, err := gorm.Open("mysql", "kirohi:cSbuS2mlGo##@/subsc?charset=utf8&parseTime=True&loc=Local")
|
168
|
+
CheckConnectError(err)
|
169
|
+
defer db.Close()
|
170
|
+
user := model.User{}
|
171
|
+
|
172
|
+
name := c.Param("name")
|
173
|
+
|
174
|
+
db.Where("name=?", name).Find(&user)
|
175
|
+
db.Delete(&user)
|
176
|
+
|
177
|
+
return c.NoContent(http.StatusOK)
|
178
|
+
}
|
179
|
+
}
|
180
|
+
|
181
|
+
func NewUser(user *model.User) {
|
182
|
+
db, err := gorm.Open("mysql", "kirohi:cSbuS2mlGo##@/subsc?charset=utf8&parseTime=True&loc=Local")
|
183
|
+
CheckConnectError(err)
|
184
|
+
defer db.Close()
|
185
|
+
db.Create(user)
|
186
|
+
}
|
187
|
+
|
188
|
+
func FindUser(u *model.User) *model.User {
|
189
|
+
db, err := gorm.Open("mysql", "kirohi:cSbuS2mlGo##@/subsc?charset=utf8&parseTime=True&loc=Local")
|
190
|
+
CheckConnectError(err)
|
191
|
+
defer db.Close()
|
192
|
+
|
193
|
+
user := model.User{}
|
194
|
+
db.Where(u).First(&user)
|
195
|
+
return &user
|
196
|
+
}
|
197
|
+
|
198
|
+
```
|
199
|
+
|
200
|
+
json
|
201
|
+
http://localhost:8888/api/v1/users/3 というurlに対して,{ "name" : "あああ" } というbodyをContent-Type application/json,POSTで投げています.
|
202
|
+
ここでidが3であるユーザーの存在は,FindByUserID(),すなわちGETリクエストで確認できています.
|
2
ログを追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -55,4 +55,29 @@
|
|
55
55
|
|
56
56
|
|
57
57
|
処理がおかしいところはありますでしょうか.
|
58
|
-
よろしくお願いします
|
58
|
+
よろしくお願いします
|
59
|
+
|
60
|
+
## 追記
|
61
|
+
ログはこのようになっております.
|
62
|
+
|
63
|
+
```log
|
64
|
+
time:2019-07-26T15:37:53+09:00 host:::1
|
65
|
+
forwardedfor: req:-
|
66
|
+
status:500
|
67
|
+
method:PUT
|
68
|
+
uri:/api/v1/users/3
|
69
|
+
size:36 referer:
|
70
|
+
ua:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36
|
71
|
+
reqtime_ns:1895431
|
72
|
+
cache:- runtime:-
|
73
|
+
apptime:-
|
74
|
+
vhost:localhost:8888
|
75
|
+
reqtime_human:1.895431ms
|
76
|
+
x-request-id: host:localhost:8888
|
77
|
+
```
|
78
|
+
|
79
|
+
下記はかなり見辛いログですが,載せておきます.
|
80
|
+
|
81
|
+
``` log
|
82
|
+
{"time":"2019-07-26T15:37:53.480874+09:00","level":"-","prefix":"echo","file":"recover.go","line":"73","message":"[PANIC RECOVER] runtime error: invalid memory address or nil pointer dereference goroutine 50 [running]:\necho/middleware.RecoverWithConfig.func1.1.1(0x156b148, 0x1000, 0xc000060000, 0x160b140, 0xc0002b2120)\n\t/Users/kirohi/go/src/echo/middleware/recover.go:71 +0xf1\npanic(0x14cc0e0, 0x195b440)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/runtime/panic.go:522 +0x1b5\ngithub.com/ia17011/subsc/backend/api.UpdateUser.func1(0x160b140, 0xc0002b2120, 0x0, 0x0)\n\t/Users/kirohi/go/src/github.com/ia17011/subsc/backend/api/user.go:74 +0x19d\ngithub.com/labstack/echo.(*Echo).add.func1(0x160b140, 0xc0002b2120, 0xc0002160a0, 0xf)\n\t/Users/kirohi/go/src/github.com/labstack/echo/echo.go:505 +0x87\ngithub.com/ia17011/subsc/backend/api/middlewares.serverHeader.func1(0x160b140, 0xc0002b2120, 0x156b148, 0x1000)\n\t/Users/kirohi/go/src/github.com/ia17011/subsc/backend/api/middlewares/mainMiddlewares.go:25 +0x235\necho/middleware.RecoverWithConfig.func1.1(0x160b140, 0xc0002b2120, 0x0, 0x0)\n\t/Users/kirohi/go/src/echo/middleware/recover.go:78 +0xd5\necho/middleware.LoggerWithConfig.func2.1(0x160b140, 0xc0002b2120, 0x3, 0x4)\n\t/Users/kirohi/go/src/echo/middleware/logger.go:119 +0x1c0\ngithub.com/labstack/echo.(*Echo).ServeHTTP(0xc00028a000, 0x15fe860, 0xc0002bc000, 0xc0001e8a00)\n\t/Users/kirohi/go/src/github.com/labstack/echo/echo.go:616 +0x22d\nnet/http.serverHandler.ServeHTTP(0xc000226750, 0x15fe860, 0xc0002bc000, 0xc0001e8a00)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/net/http/server.go:2774 +0xa8\nnet/http.(*conn).serve(0xc00021e0a0, 0x15ff560, 0xc0001ecc00)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/net/http/server.go:1878 +0x851\ncreated by net/http.(*Server).Serve\n\t/usr/local/Cellar/go/1.12.5/libexec/src/net/http/server.go:2884 +0x2f4\n\ngoroutine 1 [IO wait]:\ninternal/poll.runtime_pollWait(0x1e91ea8, 0x72, 0x0)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/runtime/netpoll.go:182 +0x56\ninternal/poll.(*pollDesc).wait(0xc000232818, 0x72, 0x0, 0x0, 0x154be4a)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/internal/poll/fd_poll_runtime.go:87 +0x9b\ninternal/poll.(*pollDesc).waitRead(...)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/internal/poll/fd_poll_runtime.go:92\ninternal/poll.(*FD).Accept(0xc000232800, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/internal/poll/fd_unix.go:384 +0x1ba\nnet.(*netFD).accept(0xc000232800, 0x30, 0x50, 0x50)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/net/fd_unix.go:238 +0x42\nnet.(*TCPListener).accept(0xc0002222f8, 0xc0f05880cb, 0xc000088000, 0xc00010dd00)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/net/tcpsock_posix.go:139 +0x32\nnet.(*TCPListener).AcceptTCP(0xc0002222f8, 0x100000001, 0xc00010dd30, 0x104c368)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/net/tcpsock.go:247 +0x48\ngithub.com/labstack/echo.tcpKeepAliveListener.Accept(0xc0002222f8, 0xc00010dd58, 0x10afda6, 0x5d3a9fc1, 0x1036bcf)\n\t/Users/kirohi/go/src/github.com/labstack/echo/echo.go:820 +0x2f\nnet/http.(*Server).Serve(0xc000226750, 0x15fe4e0, 0xc000222300, 0x0, 0x0)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/net/http/server.go:2859 +0x22d\ngithub.com/labstack/echo.(*Echo).StartServer(0xc00028a000, 0xc000226750, 0x0, 0x0)\n\t/Users/kirohi/go/src/github.com/labstack/echo/echo.go:707 +0x388\ngithub.com/labstack/echo.(*Echo).Start(...)\n\t/Users/kirohi/go/src/github.com/labstack/echo/echo.go:627\nmain.main()\n\t/Users/kirohi/go/src/github.com/ia17011/subsc/backend/main.go:16 +0x67\n\ngoroutine 51 [runnable]:\ndatabase/sql.(*DB).connectionOpener(0xc0002ca000, 0x15ff560, 0xc000220300)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/database/sql/sql.go:1000 +0xe8\ncreated by database/sql.OpenDB\n\t/usr/local/Cellar/go/1.12.5/libexec/src/database/sql/sql.go:670 +0x15e\n\ngoroutine 35 [runnable]:\ngithub.com/go-sql-driver/mysql.(*mysqlConn).startWatcher.func1(0xc0001f9080, 0xc0002c6120, 0xc0001f62a0)\n\t/Users/kirohi/go/src/github.com/go-sql-driver/mysql/connection.go:619 +0xbf\ncreated by github.com/go-sql-driver/mysql.(*mysqlConn).startWatcher\n\t/Users/kirohi/go/src/github.com/go-sql-driver/mysql/connection.go:616 +0xbe\n\ngoroutine 52 [runnable]:\ndatabase/sql.(*DB).connectionResetter(0xc0002ca000, 0x15ff560, 0xc000220300)\n\t/usr/local/Cellar/go/1.12.5/libexec/src/database/sql/sql.go:1013 +0x\n"}
|
83
|
+
```
|
1
誤字
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
【Go】echoでのUpdateで500エラーが返り実行できない
|
1
|
+
【Go】echoでのUpdate実装で500エラーが返り実行できない
|
body
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
echoでのユーザー情報を
|
3
|
+
echoでのユーザー情報をUpdateする関数の実装
|
4
4
|
|
5
5
|
http://localhost:8888/api/v1/users/:id にてPUTでユーザーを更新しようとしています
|
6
6
|
{"name" : "なまえ"} という様なjsonを送ってとりあえず処理を実行したいと思ってます.
|