在我的终端中它说错误在 D:/data lecture/Hacktive8/tesjwt/controllers/userControllers.go:26 (0xa2010f) UserRegister: 错误 := db.Debug().Create(&User).Error
并说运行时错误:无效的内存地址或零指针取消引用
这里是我的 userControllers.go
package controllers
import (
"net/http"
"github.com/gin-gonic/gin"
"tesjwt.go/database"
"tesjwt.go/helpers"
"tesjwt.go/models"
)
var appJSON = "application/json"
func UserRegister(c *gin.Context) {
db := database.GetDB()
contentType := helpers.GetContentType(c)
User := models.User{}
if contentType == appJSON {
c.ShouldBindJSON(&User)
} else {
c.ShouldBind(&User)
}
err := db.Debug().Create(&User).Error
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Bad Request",
"message": err.Error(),
})
return
}
c.JSON(http.StatusCreated, gin.H{
"id": User.ID,
"email": User.Email,
"full_name": User.FullName,
})
}
谁能帮帮我?