Задать вопрос
@afilmofog

В чем причина 500 статуса на heroku?

Развернул API на Heroku и подключил PostgreSQL. При попытке обратиться к API возникает 500 статус везде, где осуществляется работа с БД. В чем может быть причина?

CRUD модели
type User struct {
	gorm.Model
	Fullname string   `json:"fullname"`
	Birthday string   `json:"date"`
	GroupID  []Groups `json:"group_id"` 
	Login    string   `json:"login"`
	Password string   `json:"password"`
	Role     string   `json:"role"`
}

db, err := gorm.Open("postgres", "sslmode=require host=foo dbname=foo user=foo port=bar password=bar")
	if err != nil {
		fmt.Println(err)
	}

	db.AutoMigrate(&User{})



func GetAllUsers(c *gin.Context) {
	var users []User
	c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
	c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type,Token")
	if err := db.Find(&users).Error; err != nil {
	c.AbortWithStatus(404)
	fmt.Println(err)
	} else {
	c.JSON(200, users)
	}
}

func GetSingleUser(c *gin.Context) {
	c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
	c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type,Token")
	id := c.Params.ByName("id")
	var user User
	if err := db.Where("id = ?", id).First(&user).Error; err != nil {
		c.AbortWithStatus(404)
		fmt.Println(err)
	} else {
		c.JSON(200, user)
	}
}

func CreateUser(c *gin.Context) {
	c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
	c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type,Token")
	var user User
	c.BindJSON(&user)
	db.Create(&user)
	c.JSON(200, user)
}

func UpdateUser(c *gin.Context) {
	c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
	c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type,Token")
	var user User
	id := c.Params.ByName("id")
	if err := db.Where("id = ?", id).First(&user).Error; err != nil {
		c.AbortWithStatus(404)
		fmt.Println(err)
	}
	c.BindJSON(&user)
	db.Save(&user)
	c.JSON(200, user)
}

func DeleteUser(c *gin.Context) {
	c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
	c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type,Token")
	id := c.Params.ByName("id")
	var user User
	d := db.Where("id=?", id).Delete(&user)
	fmt.Println(d)
	c.JSON(200, gin.H{"id #" + id: "deleted"})
}

Логи
2019-06-03T16:15:33.081839+00:00 app[web.1]: runtime error: invalid memory address or nil pointer dereference
2019-06-03T16:15:33.081841+00:00 app[web.1]: /app/tmp/cache/go1.12.5/go/src/runtime/panic.go:82 (0x442570)
2019-06-03T16:15:33.081842+00:00 app[web.1]: /app/tmp/cache/go1.12.5/go/src/runtime/signal_unix.go:390 (0x44239f)
2019-06-03T16:15:33.081845+00:00 app[web.1]: /tmp/tmp.EQshhkWZGc/.go/src/github.com/user/golang-api/vendor/github.com/jinzhu/gorm/main.go:777 (0x9cf9a3)
2019-06-03T16:15:33.081847+00:00 app[web.1]: /tmp/tmp.EQshhkWZGc/.go/src/github.com/user/golang-api/vendor/github.com/jinzhu/gorm/main.go:181 (0x9c8cbe)
2019-06-03T16:15:33.081849+00:00 app[web.1]: /tmp/tmp.EQshhkWZGc/.go/src/github.com/user/golang-api/vendor/github.com/jinzhu/gorm/main.go:458 (0x9cce32)
2019-06-03T16:15:33.081851+00:00 app[web.1]: /tmp/tmp.EQshhkWZGc/.go/src/github.com/user/golang-api/main.go:173 (0xa1bb64)
2019-06-03T16:15:33.081857+00:00 app[web.1]: /tmp/tmp.EQshhkWZGc/.go/src/github.com/user/golang-api/vendor/github.com/gin-gonic/gin/context.go:124 (0x979649)
2019-06-03T16:15:33.081859+00:00 app[web.1]: /tmp/tmp.EQshhkWZGc/.go/src/github.com/user/golang-api/vendor/github.com/gin-gonic/gin/recovery.go:83 (0x98c919)
2019-06-03T16:15:33.081861+00:00 app[web.1]: /tmp/tmp.EQshhkWZGc/.go/src/github.com/user/golang-api/vendor/github.com/gin-gonic/gin/context.go:124 (0x979649)
2019-06-03T16:15:33.081863+00:00 app[web.1]: /tmp/tmp.EQshhkWZGc/.go/src/github.com/user/golang-api/vendor/github.com/gin-gonic/gin/logger.go:240 (0x98b9c0)
2019-06-03T16:15:33.081865+00:00 app[web.1]: /tmp/tmp.EQshhkWZGc/.go/src/github.com/user/golang-api/vendor/github.com/gin-gonic/gin/context.go:124 (0x979649)
2019-06-03T16:15:33.081867+00:00 app[web.1]: /tmp/tmp.EQshhkWZGc/.go/src/github.com/user/golang-api/vendor/github.com/gin-gonic/gin/gin.go:389 (0x982e61)
2019-06-03T16:15:33.081869+00:00 app[web.1]: /tmp/tmp.EQshhkWZGc/.go/src/github.com/user/golang-api/vendor/github.com/gin-gonic/gin/gin.go:351 (0x982693)
2019-06-03T16:15:33.081871+00:00 app[web.1]: /app/tmp/cache/go1.12.5/go/src/net/http/server.go:2774 (0x6b4f47)
2019-06-03T16:15:33.081873+00:00 app[web.1]: /app/tmp/cache/go1.12.5/go/src/net/http/server.go:1878 (0x6b0b30)
2019-06-03T16:15:33.081875+00:00 app[web.1]: /app/tmp/cache/go1.12.5/go/src/runtime/asm_amd64.s:1337 (0x459e10)
2019-06-03T16:15:33.081877+00:00 app[web.1]: 
2019-06-03T16:15:33.081886+00:00 app[web.1]: [GIN] 2019/06/03 - 16:15:33 | 500 |    1.236234ms |  213.87.152.148 | POST     /api/users/
  • Вопрос задан
  • 124 просмотра
Подписаться 2 Простой 2 комментария
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы