D
D
Damn2019-06-03 23:03:49
PostgreSQL
Damn, 2019-06-03 23:03:49

What is the reason for 500 status on heroku?

Deployed the API on Heroku and connected PostgreSQL. When you try to access the API, a 500 status occurs wherever you work with the database. What could be the reason?
CRUD models

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"})
}

Logs
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/

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question